[英]Appending to nested list in python dictionary
I have a dictionary with a nested list as values (actually thousands of entries, here just a simplification).我有一个带有嵌套列表作为值的字典(实际上有数千个条目,这里只是一个简化)。
my_dict = {'topic a': [['CategoryA', 230], ['CategoryB', 270]], 'topic b': [['CategoryA', 198], ['CategoryB', 188]]}
I further have two lists with float values (ordered and same amount as list values): list_A: [0.1, 0.1], list_B: [0.2, 0.2]
我还有两个带有浮点值的列表(有序且数量与列表值相同):
list_A: [0.1, 0.1], list_B: [0.2, 0.2]
I wish to iterate over the dictionary and lists and per iteration step append the float values of A to the first nested list of the dictionary's item and the float values of B to the second nested list of the dictionary's item.我希望遍历字典和列表,并且在每个迭代步骤 append 中,A 的浮点值到字典项的第一个嵌套列表,B 的浮点值到字典项的第二个嵌套列表。 The order has to remain as it is.
订单必须保持原样。
My desired outcome is as follows:我想要的结果如下:
{'topic a': [['CategoryA', 230, 0.1], ['CategoryB', 270, 0.2]], 'topic b': [['CategoryA', 198, 0.1], ['CategoryB', 188, 0.2]]}
I have tried so many things;我尝试了很多东西; the iteration just won't quite work.
迭代将无法正常工作。 Please see the code of one of my attempts:
请参阅我的尝试之一的代码:
my_dict = {'topic a': [['CategoryA', 230], ['CategoryB', 270]], 'topic b': [['CategoryA', 198], ['CategoryB', 188]]}
list_A = [0.1, 0.1]
list_B = [0.2, 0.2]
for element in my_dict.values():
for el in list_A:
element[0].append(el)
for el_ in list_B:
element[1].append(el_)
print(my_dict)
The undesired outcome is:不希望的结果是:
{'topic a': [['CategoryA', 230, 0.1, 0.1], ['CategoryB', 270, 0.2, 0.2]], 'topic b': [['CategoryA', 198, 0.1, 0.1], ['CategoryB', 188, 0.2, 0.2]]}
I also tried a workaround with zip, also no success it says 'TypeError: 'list' object is not callable' when trying to convert zip to list.我还尝试了使用 zip 的解决方法,但在尝试将 zip 转换为列表时,它显示“TypeError: 'list' object is not callable”也没有成功。
test = []
for element in my_dict.values():
test = zip(element[0], list_A)
test = list(test)
print(test)
For my first example I actually understand why I get the undesired outcome but just can't get to the desired one;对于我的第一个示例,我实际上理解为什么我得到了不想要的结果,但无法达到想要的结果; for the zip attempt I do not see the problem.
对于 zip 尝试,我没有看到问题。 Any (pythonic) ideas?
任何(pythonic)想法? - Very much appreciated!
- 非常感谢!
Update: Dictionary transformed to list, still not working;更新:字典转换为列表,仍然无法正常工作; what am I missing?
我错过了什么?
my_list = []
for key, value in my_dict.items():
temp = [key,value]
my_list.append(temp)
print(my_list)
# output: [['topic a', [['CategoryA', 230], ['CategoryB', 270]]], ['topic b', [['CategoryA', 198], ['CategoryB', 188]]]]
list_A = [0.1, 0.1]
list_B = [0.2, 0.2]
for element in my_list:
for el in list_A:
element[1][0].append(el)
for el_ in list_B:
element[1][1].append(el_)
print(my_list)
# output: [['topic a', [['CategoryA', 230, 0.1, 0.1], ['CategoryB', 270, 0.2, 0.2]]], ['topic b', [['CategoryA', 198, 0.1, 0.1], ['CategoryB', 188, 0.2, 0.2]]]]
test = []
for element in my_list:
test = zip(element[1][0], list_A)
test = list(test)
print(test)
# output: TypeError: 'list' object is not callable
Of course, since there are infinitely many interpretations of your request, I don't think this is actually what you want, but let's start from here:当然,既然你的请求有无数种解释,我认为这并不是你真正想要的,但让我们从这里开始:
my_dict = {'topic a': [['CategoryA', 230], ['CategoryB', 270]],
'topic b': [['CategoryA', 198], ['CategoryB', 188]]}
list_A = [0.1, 0.1]
list_B = [0.2, 0.2]
desired = {'topic a': [['CategoryA', 230, 0.1], ['CategoryB', 270, 0.2]],
'topic b': [['CategoryA', 198, 0.1], ['CategoryB', 188, 0.2]]}
for outerlist, column in zip(my_dict.values(), zip(list_A, list_B)):
for nestedlist, floatvalue in zip(outerlist, column):
nestedlist.append(floatvalue)
assert my_dict == desired
This will work:这将起作用:
dict_keys = list(my_dict.keys())
for i in range(0, len(list_A)):
val_A = list_A[i]
val_B = list_B[i]
my_dict[dict_keys[i]][0].append(val_A)
my_dict[dict_keys[i]][1].append(val_B)
print(my_dict)
# {'topic a': [['CategoryA', 230, 0.1], ['CategoryB', 270, 0.2]], 'topic b': [['CategoryA', 198, 0.1], ['CategoryB', 188, 0.2]]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.