【问题标题】:In python. How do I have a user change a dictionary value, when that dictionary is in a class?在蟒蛇。当字典在类中时,如何让用户更改字典值?
【发布时间】:2011-08-02 00:46:22
【问题描述】:

所以我有一个类似的问题,在另一个帖子中得到了回答。

How do I update a dictionary value having the user choose the key to update and then the new value, in Python?

基本上,如何通过 raw_input 更改嵌套字典值。我使用了该解决方案并且效果很好,但我想使用类编写程序。所以我创建了一个类,它使用基本相同的代码编辑字典的方法,但是当我尝试在类方法中运行它时,它现在给了我一个“关键错误”。

因此,在主要功能中,上述链接问题中的解决方案效果很好。但是在类方法中:

class team: # create a class where each team will be an instance
  def __init__(self, name):
    self.name = name #name of team will be passed from main
    self.list_of_players = [] # create a list of the players
    self.position1 = {} # create a dictionary for each of the positions on that team
    self.position2 = {}
    self.roster = [self.position1, self.position2]

  def addplayer(self, player_name): # the name of the player is passed to this method from main
    print 'add stats' # fill out the appropriate stats through raw_input 
    stat1 = raw_input('stat1: ')
    stat2 = raw_input('stat2: ')
    pos = raw_input('POS: ')
    vars()[player_name] = {'stat1' : stat1, 'stat2' : stat2, 'POS' : pos} #create a dictionary 
    # for the player where all his stats are kept
    player = {player_name : vars()[player_name]} # create a dictionary that will show the 
    # player's name as a string and his stats which are held in the dictionary named after him
    self.list_of_players.append(player) # append the new player to the list of players
    if pos == 'p1': # add the player and his stats to the appropriate position on the team
      self.position1[player_name] = player
    elif pos == 'p2':
      self.position2[player_name] = player
    else:
      pass

  def editplayer(self, player_name): # player's name is passed to the edit function from main
    print self.list_of_players # player's name shows up in the list of players for the team
    edit_stat = raw_input('which stat? ') # choose which stat(key) to edit via raw input
    new_value = raw_input('new value: ') # choose the new value to apply to the chosen key
    vars()[player_name][edit_stat] = new_value # here is where it gives a key error! this worked 
 #in fact even trying to call and print the players name gives the key error. 
    #player = vars()[player_name]
    #print player

def main(): # the main function
  loop1 = 0 # creating a loop so one can come back and edit the teams after creating them
  list_of_teams = [] # initializing list of teams
  while loop1 < 1:
    print list_of_teams # show the user what teams are available to choose from
    team_option = raw_input('new team or old: ') # create a new team or work with an old one
    if team_option == 'new':
      team_name = raw_input('team name? ') # get the team name from raw_input
      vars()[team_name] = team(team_name) #create an instance of this team name
      list_of_teams.append(team_name) # add the team to the list
    else:
      team_name = raw_input('which team? ') # choose which existing team to work with
      player_choice = raw_input('new player or old? ') # choose to create or edit existing player
      player_name = raw_input('player_name? ') # choose which player from raw_input
      if player_choice == 'new':
        vars()[team_name].addplayer(player_name) # give player_name to addplayer method 
        print vars()[team_name].list_of_players # shows the new player in the appropriate
        # instance's roster. This method seems to be working fine
      else:
        vars()[team_name].editplayer(player_name) # gives the player's name to the editplayer
        # method for the appropriate instance.  But the player name just raises a key error in
        # edit player method.  I am baffled.
        print vars()[team_name].list_of_players
if __name__ == '__main__':
  main()

当它只是一个长函数时,它起作用了,但看起来像一场灾难。试图学习更好的 OOP 实践,但我不知道如何通过玩家的名字调用该字典来更改值。在过去的几天里,我一直在复习关于类和字典的教程和问题,但显然我误解了一些关于变量如何从函数传递到方法的问题。

它甚至不会将字典 vars()[player_name] 分配给要打印的 var,这意味着它无法将其识别为我认为是在 addplayer 方法中创建的字典。但是它仍然在玩家列表中列出该字典的事实意味着它存在于该实例中。那么,当我尝试在 editplayer 方法中解决它时,为什么它不能识别它呢?以及如何调用在一种方法中创建的嵌入字典,以在第二种方法中更改该字典中的值?

Karl 指出了需要澄清的要点:这就是我想要的属性。

self.name-我想要为每个创建的团队创建一个实例

self.list of player - 每支球队都应该有自己的球员名单,这些球员是保存该人统计数据的字典。所以 team1 应该有自己的列表。 team2 不同的列表等

self.position1/2 - 每支球队的球员将被归档在他们的不同位置字典中。所以球员 joe montana 的统计字典可以在该球队的四分卫字典中找到

self.roster - 应该是该团队按位置分组的名册。所以打印 team1.roster 的调用应该打印那些按位置分组的球员

【问题讨论】:

  • 首先要准确解释您希望在每个属性中得到哪些数据。
  • self.name- 我想为每支球队创建一个实例,我创建了 self.list of player - 每个球队都应该有自己的球员名单,这些球员是保存该人统计数据的字典。所以 team1 应该有自己的列表。 team2 一个不同的列表等 self.position1/2 - 每支球队的球员将被归档在他们不同的位置字典中。因此球员 joe montana 的统计字典会在该球队的四分卫字典 self.roster 中找到 - 应该是该球队按位置分组的名册。所以打印 team1.roster 的调用应该打印那些按位置分组的球员
  • 听起来你真正想做的是在球员和他们的位置之间建立双向映射......

标签: python class dictionary raw-input


【解决方案1】:

1) vars()函数内的局部变量的字典。

当您在 Python 中的方法中时,您调用该方法的对象的内容是不是局部变量。这就是为什么你必须有一个self 参数。

如果您想按姓名查找球员,请这样做。没有玩家列表,而是玩家的字典。

2) vars() 是您几乎不应该使用的东西。使用它是为了让您可以假装字符串是变量名。您不需要为您在此处所做的任何事情执行此操作。事实上,在您使用变量的大多数地方,您根本不需要变量。在这里,您需要了解的不仅仅是 OO。

以这部分为例:

vars()[team_name] = team(team_name)
list_of_teams.append(team_name)

不要尝试按名称记住vars() 中的团队,而是再次按名称查找团队。有一个团队的字典而不是一个列表。要获取团队的名称,您只需打印字典的键即可。

简单胜于复杂。动态创建变量很复杂。使用字典很简单。


我讨厌用勺子喂这么多代码,但这似乎是在这段时间内获得想法的唯一方法(s - 我并没有真正说出以上所有内容):

# Just like we want a class to represent teams, since those are "a thing" in our
# program, we want one for each player as well.

class player(object):
  __slots__ = ['name', 'stats', 'pos']
  def __init__(self, name, stats, pos):
    self.name = name
    self.stats = stats
    self.pos = pos


# Asking the user for information to create an object is not the responsibility of
# that class. We should use external functions for this.
def create_player(name):
  print 'add stats' # fill out the appropriate stats through raw_input 
  stat1 = raw_input('stat1: ')
  stat2 = raw_input('stat2: ')
  pos = raw_input('POS: ')
  # Now we create and return the 'player' object.
  return player(name, {'stat1': stat1, 'stat2': stat2}, pos)


class team(object):
  __slots__ = ['name_to_player', 'position_to_player']
  def __init__(self):
    # We don't make any lists, just dicts, because we want to use them primarily
    # for lookup. Notice how I've named the attributes. In particular, I **don't**
    # talk about type names. That's just an implementation detail. What we care about
    # is how they work: you put a name in, get a player out.
    self.name_to_player = {}
    self.position_to_player = {}

  # Again, we don't ask the questions here; this just actually adds the player.
  def add_player(self, player):
    self.name_to_player[player.name] = player
    self.position_to_player[player.pos] = player

  # Again, we don't ask the questions here; this just does the actual edit.
  def edit_player(self, name, stat, new_value):
    self.name_to_player[name].stats[stat] = new_value


def main(): # the main function
  teams = {} # dict from team name to team object.
  while True:
    print teams.keys()
    # Your human interface was needlessly awkward here; you know from the supplied name
    # whether it's a new team or an old one, because it will or won't be in your
    # existing set of teams. Similarly for players.
    team_name = raw_input('team name? ')
    if team_name not in teams.keys():
      teams[team_name] = team() # create a new team
    else: # edit an existing one
      team = teams[team_name]
      player_name = raw_input('player name? ')
      if player_name in team.name_to_player.keys(): # edit an existing player
        stat = raw_input("stat? ")
        value = raw_input("value? ")
        team.edit_player(player_name, stat, value)
      else: # add a new player
        team.add_player(create_player(player_name))

if __name__ == '__main__':
  main()

仍然没有做所有“正确”的事情,但它应该让你现在考虑的足够多。

【讨论】:

  • 当然,我要学习的不仅仅是 OO。这就是我在这里的原因。在该示例中发生了两件不同的事情。一个我在 main() 中有一个团队列表,我正在向其中添加 team_name。我链接的另一个问题中的问题是,当我希望用户识别字典时,用户输入是一个字符串。解决方案是 vars()。我不想传递 str 'player',因为当我尝试调用字典时,它给了我错误“'str' object does not support item assignment。
  • 这就是我使用 var() 的原因。我希望用户输入字符串信息,但程序将其解释为字典名称。
  • 只是为了澄清,也许你已经明白了,我只是错过了你的观点。但需要澄清的是,在用户创建团队之前,字典中没有团队。用户必须给程序 dict 变量名称,他们不能只传递字符串键来调用匹配值。如果你已经明白了,对不起。
  • 感谢您花时间提供帮助。我会研究你用勺子喂的东西,看看这些想法是否能传达给我
【解决方案2】:

首先,Key error 附带的回溯会告诉您程序中的哪一行触发了它,如果通过查看代码不明显,那么在该行之前插入一个打印语句应该可以使其明显.

其次,您将用户输入用作键。用户输入不可靠。你一直有关键错误,所以你的代码应该处理这个问题,要么使用try:except:捕获异常,要么在实际使用之前检查每次使用if key in mydict:查找字典的键。

第三,你用 vars() 做的事情非常非常奇怪。如果您的应用程序使用全局变量,那么它应该知道名称并且无需引用 vars。是不是忘记在某个方法中声明一个全局变量?

def method(self,name):
    global bigdict
    bigdict[name] = "set at least one time"

【讨论】:

  • 当然应该是try:...except KeyError:。 'Bare' excepts 可以隐藏各种错误。
  • 绝对!我不打算按字面意思使用,只是作为手册的参考。
  • @迈克尔。首先感谢您的回复和尝试提供帮助。对此,我真的非常感激。我指出了错误所在的行,并显示了我试图简单地调用和打印的位置(它在注释掉的部分和编辑函数的末尾。我知道需要捕获用户输入,但是,当我我自己正在运行它,试图给出准确的输入,但它无法识别与调用字典的名称相同的名称。所以我认为我的问题在于一种方法无法识别另一种方法中的字典?
  • 我还运行了“if ... in list: ... else print 'negative'”,它会打印“negative”。我已经完成了:打印列表... if in list: else: print 'negative' 并打印显示字典名称的列表.. 并打印 'negative'
  • 指示错误发生位置的正常方法是包含 Python 打印出的回溯。
猜你喜欢
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 2022-06-17
  • 2022-11-22
  • 1970-01-01
  • 2019-07-17
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多