寻找向子词典中的列表添加值的最简单方法
Looking for the easiest way to add values to the list in subdictionaries
寻找在子词典中添加值的最简单方法。一个例子:
dict1 = {
"l": {"a": 1, "b": 2, "c": 3},
"s": {"a": 4, "b": 5, "c": 6},
"d": {"a": 7, "b": 8, "c": 9} }
我想创建输出:
{"a": [1, 4, 7], "b": [2, 5, 8], "c": [3, 6, 9]}
不起作用的东西:
from collections import defaultdict
dict1 = {
"l": {"a": 1, "b": 2, "c": 3},
"s": {"a": 4, "b": 5, "c": 6},
"d": {"a": 7, "b": 8, "c": 9} }
new_dict = defaultdict(list)
for k in dict1:
d2 = dict1.get(k)
for k, v in d2.items():
new_dict[k].append[v]
输出:类型错误:'builtin_function_or_method'对象不可订阅
我也做过这样的事情:
dict1 = {
"l": {"a": 1, "b": 2, "c": 3},
"s": {"a": 4, "b": 5, "c": 6},
"d": {"a": 7, "b": 8, "c": 9} }
final_dict = {}
# list_dict = []
for k, v in dict1.items():
dict_new = dict1[k]
list_dict = []
for new_k, new_v in dict_new.items():
list_dict.append(new_v)
final_dict[new_k] = list_dict
print("list_dict ", list_dict)
print("final_dict", final_dict)
...但是 输出 是:
{'a': [7, 8, 9], 'b': [7, 8, 9], 'c': [7, 8, 9]}
当我把 list_dict = []
放在 for 循环外面时,输出是:
{'a': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'b': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'c': [1, 2, 3, 4, 5, 6, 7, 8, 9]}
我想知道我是否需要某种递归方法来按所有子键(a、b、c)分组并附加所有相关值,或者可能需要字典理解。
输出表明您想要遍历 dict1
的值。一种选择是使用 collections.defaultdict
:
from collections import defaultdict
out = defaultdict(list)
for d in dict1.values():
for k,v in d.items():
out[k].append(v)
out = dict(out)
另一种选择是使用 cytoolz.dicttoolz.merge_with
:
from cytoolz.dicttoolz import merge_with
out = merge_with(list, *dict1.values())
另一种选择是在理解中使用 operator.itemgetter
:
from operator import itemgetter
vals = list(dict1.values())
keys = chain.from_iterable(map(dict.keys, vals))
out = {k: list(map(itemgetter(k), vals)) for k in keys}
输出:
{'a': [1, 4, 7], 'b': [2, 5, 8], 'c': [3, 6, 9]}
new_dict[k].append[v]
应该是 new_dict[k].append(v)
- 你没有调用方法(你应该这样),你试图将它作为 list/dict 索引。也没有理由在迭代后立即在字典中查找 k
的值 - 您可以使用 values()
来仅获取值。
for inner_dict in dict1.values():
for k, v in inner_dict.items():
new_dict[k].append(v)
添加到以前的答案中,如果您希望在 one-liner 中执行此操作,您可以这样做:
dict1 = {
"l": {"a": 1, "b": 2, "c": 3},
"s": {"a": 4, "b": 5, "c": 6},
"d": {"a": 7, "b": 8, "c": 9}
}
out = dict(zip(dict1.keys(), map(list, zip(*map(dict.values, dict1.values())))))
print(out)
输出:
{'l': [1, 4, 7], 's': [2, 5, 8], 'd': [3, 6, 9]}
寻找在子词典中添加值的最简单方法。一个例子:
dict1 = {
"l": {"a": 1, "b": 2, "c": 3},
"s": {"a": 4, "b": 5, "c": 6},
"d": {"a": 7, "b": 8, "c": 9} }
我想创建输出:
{"a": [1, 4, 7], "b": [2, 5, 8], "c": [3, 6, 9]}
不起作用的东西:
from collections import defaultdict
dict1 = {
"l": {"a": 1, "b": 2, "c": 3},
"s": {"a": 4, "b": 5, "c": 6},
"d": {"a": 7, "b": 8, "c": 9} }
new_dict = defaultdict(list)
for k in dict1:
d2 = dict1.get(k)
for k, v in d2.items():
new_dict[k].append[v]
输出:类型错误:'builtin_function_or_method'对象不可订阅
我也做过这样的事情:
dict1 = {
"l": {"a": 1, "b": 2, "c": 3},
"s": {"a": 4, "b": 5, "c": 6},
"d": {"a": 7, "b": 8, "c": 9} }
final_dict = {}
# list_dict = []
for k, v in dict1.items():
dict_new = dict1[k]
list_dict = []
for new_k, new_v in dict_new.items():
list_dict.append(new_v)
final_dict[new_k] = list_dict
print("list_dict ", list_dict)
print("final_dict", final_dict)
...但是 输出 是:
{'a': [7, 8, 9], 'b': [7, 8, 9], 'c': [7, 8, 9]}
当我把 list_dict = []
放在 for 循环外面时,输出是:
{'a': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'b': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'c': [1, 2, 3, 4, 5, 6, 7, 8, 9]}
我想知道我是否需要某种递归方法来按所有子键(a、b、c)分组并附加所有相关值,或者可能需要字典理解。
输出表明您想要遍历 dict1
的值。一种选择是使用 collections.defaultdict
:
from collections import defaultdict
out = defaultdict(list)
for d in dict1.values():
for k,v in d.items():
out[k].append(v)
out = dict(out)
另一种选择是使用 cytoolz.dicttoolz.merge_with
:
from cytoolz.dicttoolz import merge_with
out = merge_with(list, *dict1.values())
另一种选择是在理解中使用 operator.itemgetter
:
from operator import itemgetter
vals = list(dict1.values())
keys = chain.from_iterable(map(dict.keys, vals))
out = {k: list(map(itemgetter(k), vals)) for k in keys}
输出:
{'a': [1, 4, 7], 'b': [2, 5, 8], 'c': [3, 6, 9]}
new_dict[k].append[v]
应该是 new_dict[k].append(v)
- 你没有调用方法(你应该这样),你试图将它作为 list/dict 索引。也没有理由在迭代后立即在字典中查找 k
的值 - 您可以使用 values()
来仅获取值。
for inner_dict in dict1.values():
for k, v in inner_dict.items():
new_dict[k].append(v)
添加到以前的答案中,如果您希望在 one-liner 中执行此操作,您可以这样做:
dict1 = {
"l": {"a": 1, "b": 2, "c": 3},
"s": {"a": 4, "b": 5, "c": 6},
"d": {"a": 7, "b": 8, "c": 9}
}
out = dict(zip(dict1.keys(), map(list, zip(*map(dict.values, dict1.values())))))
print(out)
输出:
{'l': [1, 4, 7], 's': [2, 5, 8], 'd': [3, 6, 9]}