英文:
How can I update values in a dictionary that is stored by a list, from strings to integers in Python?
问题
You can change the values inside the dictionary from strings to integers using a nested loop. Here's the corrected code:
for i in ct_dict:
ct_dict[i] = [int(j) for j in ct_dict[i]]
This code will convert all the values in the dictionary to integers without causing the "dictionary changed size during iteration" error.
英文:
ct_dict = {
'fr': ['55410', '94560', '52533', '63040', '52533'],
'de': ['73546', '64090', '60938', '75648', '60938']}
How can i change the values inside the dictionary from string to integer? For example: ['55410', '94560', '52533', '63040', '52533']
to [55410, 94560, 52533, 63040, 52533]
I tried using 2 for loops with update method but got this error:
RuntimeError: dictionary changed size during iteration
Code that got the error:
for i in ct_dict:
for j in ct_dict[i]:
ct_dict.update({j:int(j)})
答案1
得分: 1
ct_dict
的键是i
,而不是j
,因此在.update()
方法中不应该使用j
。
但是,如果你只是更新一个键,可以直接将值赋给该键,而不必使用.update()
。
你需要将int()
映射到列表的所有元素上。
for i, vals in ct_dict.items():
ct_dict[i] = list(map(int, vals))
英文:
The keys of ct_dict
are i
, not j
, so you shouldn't use j
in the .update()
method.
But if you're just updating one key, just assign to that key rather than using .update()
.
You need to map int()
over all the elements of the list.
for i, vals in ct_dict.items():
ct_dict[i] = list(map(int, vals))
答案2
得分: 1
你实际上不需要更新字典,只需直接更改列表中的值:
for nums in ct_dict.values():
nums[:] = map(int, nums)
英文:
You don't actually need to update the dictionary, just change the values in the lists directly:
for nums in ct_dict.values():
nums[:] = map(int,nums)
答案3
得分: 0
您遇到错误是因为您正在尝试在for
循环中修改您正在用作引用的字典,而在执行循环时,您不能这样做。
解决方法
假设:
ct_dict = {
"fr": ["55410", "94560", "52533", "63040", "52533"],
"de": ["73546", "64090", "60938", "75648", "60938"],
}
最佳选项是使用字典推导式:
new_ct_dict = {key: [int(i) for i in values] for key, values in ct_dict.items()}
但您也可以这样做:
new_ct_dict = {}
for key, values in ct_dict.items():
new_ct_dict.update({key: []})
for i in values:
new_ct_dict[key].append(int(i))
结果:
{'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
英文:
You're getting an error because you're trying to modify the dict you're using as a reference in the for loop, while the loop is executing, and you can't do that.
Solution
Assuming:
ct_dict = {
"fr": ["55410", "94560", "52533", "63040", "52533"],
"de": ["73546", "64090", "60938", "75648", "60938"],
}
Best option is use to dict comprehension:
new_ct_dict = {key: [int(i) for i in values] for key, values in ct_dict.items()}
But you can also do:
new_ct_dict = {}
for key, values in ct_dict.items():
new_ct_dict.update({key: []})
for i in values:
new_ct_dict[key].append(int(i))
Result:
{'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
答案4
得分: 0
使用 for
循环和列表推导:
my_dict = {'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
new_dict = {}
for key, val in my_dict.items():
new_dict[key] = [int(item) for item in val]
print(new_dict) # {'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
或者,如果你不想创建另一个 dict
:
my_dict = {'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
for key, val in my_dict.items():
my_dict[key] = [int(item) for item in val]
print(my_dict) # {'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
英文:
Use a for
loop and a list comprehension:
my_dict = {'fr': ['55410', '94560', '52533', '63040', '52533'], 'de': ['73546', '64090', '60938', '75648', '60938']}
new_dict = {}
for key, val in my_dict.items():
new_dict[key] = [int(item) for item in val]
print(new_dict) # {'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
Or, if you'd rather not create another dict
:
my_dict = {'fr': ['55410', '94560', '52533', '63040', '52533'], 'de': ['73546', '64090', '60938', '75648', '60938']}
for key, val in my_dict.items():
my_dict[key] = [int(item) for item in val]
print(my_dict) # {'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
For more on list comprehensions see: https://www.w3schools.com/python/python_lists_comprehension.asp
For more on the dict.items()
method see: https://www.w3schools.com/python/ref_dictionary_items.asp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论