[英]How to update an item in a list that is value to a key in a dictionary?
So its something like this: 所以它是这样的:
{'golden': [1],
'crazy': [1]}
and then probably update it to 然后可能将其更新为
{'golden': [2],
'crazy': [2]}
I just want to know how I can access that list in the value of the dictionary and then change it from 1 to 2. 我只想知道如何访问字典值中的列表,然后将其从1更改为2。
You can access dict items even if they are lists the same way, so 即使它们是列表,您也可以访问字典项,因此
dict['golden']
returns that array. 返回该数组。 If you want to set the first item in the array to 0, you would do it the same way you would normally update list values:
如果要将数组中的第一项设置为0,则可以使用通常更新列表值的方式进行操作:
dict['golden'][0] = 2
Or append simpmly by doing 或通过添加简单
dict['golden'].append(2)
If you never add to that list though, it doesn't make sense for your dict to be list objects, it might as well just be an integer 如果您从不添加该列表,那么将dict用作列表对象就没有意义,它也可能只是整数
You can do something like this: 您可以执行以下操作:
your_dict['golden'][0] += 1
your_dict['crazy'][0] += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.