如何更新具有用户选择要更新的密钥的字典值,然后在Python中更新新值? [英] How do I update a dictionary value having the user choose the key to update and then the new value, in Python?

查看:133
本文介绍了如何更新具有用户选择要更新的密钥的字典值,然后在Python中更新新值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,我的兄弟和我可以从我们的足球比赛名单中输入和编辑信息,以比较球队和管理球员等。
这是我第一次尝试过的大项目



我在字典中有一个嵌套字典,我可以让用户创建字典等。但是当我尝试使用'user'(通过raw_input)回去编辑他们我被卡住了。
下面我试图把一个简化版本的代码放在我认为与我的错误相关的。如果我需要放下一个完整的版本让我知道。

  player1 = {'stat1':A,'stat2' 2,'stat3':3} #existing玩家是字典
player2 = {'stat1':A,'stat2':2,'stat3':3}#包含stat的名称及其值
position1 = {'player1':player1}#在每个位置,字符串(播放器的名称)是键,
position2 = {'player2':player2}#包含statisics的类似命名的dict是值
position = raw_input('哪个位置?')#用户选择要编辑的位置
如果position =='position1':
打印position1#显示用户可以选择哪些玩家那个位置
player = raw_input('哪个玩家?')#用户从那个位置选择玩家可用的
如果player == player1:
打印播放器#显示用户播放器的当前统计信息他们选择
edit_query = raw_input('你需要编辑一个或多个o这些数据?')
editloop = 0
而editloop< 1:#while循环允许多个stats编辑
如果edit_query =='yes':
stat_to_edit = raw_input('哪个stat?(如果你完成类型done)')
如果stat_to_edit =='done':#end while循环用于统计编辑
editloop = editloop +1
else:
new_value = raw_input('new_value:')#user插入新值

#到这里一切正在工作。
#在以下行中,玩家应该给出
#字典的名称来更改(player1或player2)
#stat_to_edit应该给出要更改匹配值的键
#和new_value应该更新stastic
#但是我得到TypeError'str'对象不支持项目赋值

player [stat_to_edit] = new_value #update statistic
else :#end loop if not stat editing is wanted
fooedit = fooedit + 1

当我说应该给...等等,我的意思是说我要它给..



总而言之,我想让用户选择播放器编辑,选择要编辑的统计数据,然后选择新值

解决方案

问题似乎在这行之后

  player = raw_input('哪个玩家?')

播放器将是字符串,包含用户已输入,而字典,例如 player1 。这解释了为什么Python无法分配给它。你可以这样写:

  player = raw_input('哪个玩家?')
如果player ==' player1':#这些都是字符串!
current_player = player1#这是字典!
....
current_player [...] = ...#更改字典

另外请注意,对于一个名称的Python赋值通常不会有复制对象,而只能为现有对象添加另一个名称。考虑这个例子(从Python控制台):

 >>> a = {'1':1} 
>>>> a
{'1':1}
>>> b = a
>>>> b
{'1':1}
>>> b ['1'] = 2
>>>> b
{'1':2}
>>> a
{'1':2}
>>>


I am trying to write a program where my brother and I can enter and edit information from our football game rosters to compare teams and manage players, etc. This is my first 'big' project i've tried.

I have a nested dictionary inside a dictionary, I'm able to get the user to create the dictionaries etc. But when i try to have the 'user' (via raw_input) go back to edit them I get stuck. Below i tried to put a simplified version of the code down of what i think is relevant to my error. If I need to put down a full version let me know.

player1 = {'stat1' : A, 'stat2' : 2, 'stat3' : 3} #existing players are the dictionaries 
player2 = {'stat1' : A, 'stat2' : 2, 'stat3' : 3} # containing the name of stat and its value
position1 = {'player1' : player1} # in each position the string (name of player) is the key and
position2 = {'player2' : player2} # the similarly named dict containing the statisics is the value
position = raw_input('which position? ') # user chooses which position to edit
if position == 'position1':
  print position1 # shows user what players are available to choose from in that position
  player = raw_input('which player? ') #user chooses player from available at that position
  if player == player1:
    print player # shows user the current stats for the player they chose
    edit_query = raw_input('Do you need to edit one or more of these stats? ')
    editloop = 0
    while editloop < 1: # while loop to allow multiple stats editing
      if edit_query == 'yes': 
        stat_to_edit = raw_input('Which stat? (If you are done type "done") ')
          if stat_to_edit == 'done': #end while loop for stat editing
            editloop = editloop +1
          else:
            new_value = raw_input('new_value: ') #user inserts new value

# up to here everything is working. 
# in the following line, player should give the name of the
# dictionary to change (either player1 or player2) 
# stat_to_edit should give the key where the matching value is to be changed
# and new_value should update the stastic
# however I get TypeError 'str' object does not support item assignment

            player[stat_to_edit] = new_value #update statistic
      else:  # end loop if no stat editing is wanted
        fooedit = fooedit + 1

of course when i say "should give..." etc I mean to say "I want it to give.."

In summary I want the user to choose the player to edit, choose the stat to edit, then choose the new value

解决方案

The problems seems that after this line

player = raw_input('which player? ')

the player will be the string, containing what the user have typed in, and not the dictionary, like player1. This explains, why Python fails to assign to its part. You can instead write like this:

player = raw_input('which player? ')
if player == 'player1': # these are strings!
  current_player = player1 # this is dictionary!
  ....
  current_player[...] = ... # change the dictionary

Also note, that the Python assignment to a name typically does no copy object, but only adds another name for the same existing object. Consider this example (from Python console):

>>> a = {'1': 1}
>>> a
{'1': 1}
>>> b = a
>>> b
{'1': 1}
>>> b['1'] = 2
>>> b
{'1': 2}
>>> a
{'1': 2}
>>>

这篇关于如何更新具有用户选择要更新的密钥的字典值,然后在Python中更新新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆