1

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

如何在 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 - 应该是该团队按位置分组的名册。所以调用 print team1.roster 应该打印那些按位置分组的球员

4

2 回答 2

1

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

其次,您使用用户输入作为键。用户输入不可靠。您始终遇到关键错误,因此您的代码应该处理该错误,方法是使用捕获异常,或者在实际使用密钥查找字典之前try: except:检查每次使用。if key in mydict:

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

def method(self,name):
    global bigdict
    bigdict[name] = "set at least one time"
于 2011-08-02T01:01:22.800 回答
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()

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

于 2011-08-02T01:30:08.080 回答