我有一个名为my_list
的列表列表:
['sit', (1, 1)]
['laboris', (2, 1)]
['nisi', (2, 1)]
['est', (4, 1)]
['qui', (4, 1)]
['cillum', (3, 1)]
['voluptate', (3, 1)]
['eu', (3, 1)]
['irure', (3, 1)]
['sunt', (4, 1)]
['reprehenderit', (3, 1)]
['nulla', (3, 1)]
['sint', (4, 1)]
['fugiat', (3, 1)]
['dolore', (2, 1)]
['dolore', (3, 1)]
['enim', (2, 1)]
['occaecat', (4, 1)]
['tempor', (2, 1)]
['commodo', (2, 1)]
['non', (4, 1)]
['minim', (2, 1)]
['aute', (3, 1)]
['ut', (2, 2)]
['ex', (2, 1)]
['deserunt', (4, 1)]
['ea', (2, 1)]
['eiusmod', (2, 1)]
['culpa', (4, 1)]
['labore', (2, 1)]
['mollit', (4, 1)]
['officia', (4, 1)]
['cupidatat', (4, 1)]
['adipiscing', (2, 1)]
['amet', (1, 1)]
['et', (2, 1)]
['ad', (2, 1)]
['consectetur', (2, 1)]
['anim', (4, 1)]
['magna', (2, 1)]
['quis', (2, 1)]
['ullamco', (2, 1)]
['dolor', (1, 1)]
['dolor', (3, 1)]
['aliquip', (2, 1)]
['velit', (3, 1)]
['ipsum', (1, 1)]
['incididunt', (2, 1)]
['sed', (2, 1)]
['id', (4, 1)]
['esse', (3, 1)]
['exercitation', (2, 1)]
['nostrud', (2, 1)]
我尝试过:
d = {}
for item in all_lists:
d[item[0]] = item[1:]
print (d)
但这会覆盖一个密钥,而不是更新该值。例如,dolor
变为:{'dolor': [(3,1)]
而不是预期的目标:{'dolor': (3,1), (1,1), etc...}
理想情况下,字典形状将不包含元组列表作为值,但是如果需要的话。
如何将列表列表转换为我想要的格式的字典?
我已经观察到Python: List of lists to dictionary,但是这使我产生了目前的错误信息。
答案 0 :(得分:3)
使用defaultdict功能:
https://docs.python.org/2/library/collections.html#collections.defaultdict
未经测试的代码!
d = defaultdict(list)
for item in all_lists:
d[item[0]].append(item[1:])
print (d)
答案 1 :(得分:1)
如果键已经排序(看起来像这样),则可以使用itertools.groupby
:
import itertools as it
result = {k: [x[1] for x in v] for k, v in it.groupby(test, key=lambda x: x[0])}
答案 2 :(得分:0)
其中一种方法是,如果d
缺少此键且值为空列表[]
,则添加新的键值对。然后将新值附加到该列表。
Python有setdefault
可以做到这一点。
all_lists = [
['sit', (1, 1)],
['sit', (2, 2)],
['laboris', (2, 1)]
]
d = {}
for key, new_value in all_lists:
values = d.setdefault(key, [])
values.append(new_value)
print(d)
{
'sit': [(1, 1), (2, 2)],
'laboris': [(2, 1)]
}
答案 3 :(得分:-1)
这很烦人,但是您可以通过快速调用构造函数将list
更改为tuple
。
a = [
['sit', (1, 1)],
['laboris', (2, 1)],
['nisi', (2, 1)],
['est', (4, 1)],
['qui', (4, 1)],
['cillum', (3, 1)],
['voluptate', (3, 1)],
['eu', (3, 1)],
['irure', (3, 1)],
['sunt', (4, 1)],
['reprehenderit', (3, 1)],
['nulla', (3, 1)],
['sint', (4, 1)],
['fugiat', (3, 1)],
['dolore', (2, 1)],
['dolore', (3, 1)],
['enim', (2, 1)],
['occaecat', (4, 1)],
['tempor', (2, 1)],
['commodo', (2, 1)],
['non', (4, 1)],
['minim', (2, 1)],
['aute', (3, 1)],
['ut', (2, 2)],
['ex', (2, 1)],
['deserunt', (4, 1)],
['ea', (2, 1)],
['eiusmod', (2, 1)],
['culpa', (4, 1)],
['labore', (2, 1)],
['mollit', (4, 1)],
['officia', (4, 1)],
['cupidatat', (4, 1)],
['adipiscing', (2, 1)],
['amet', (1, 1)],
['et', (2, 1)],
['ad', (2, 1)],
['consectetur', (2, 1)],
['anim', (4, 1)],
['magna', (2, 1)],
['quis', (2, 1)],
['ullamco', (2, 1)],
['dolor', (1, 1)],
['dolor', (3, 1)],
['aliquip', (2, 1)],
['velit', (3, 1)],
['ipsum', (1, 1)],
['incididunt', (2, 1)],
['sed', (2, 1)],
['id', (4, 1)],
['esse', (3, 1)],
['exercitation', (2, 1)],
['nostrud', (2, 1)]
]
final = {}
for l in a:
final[l[0]]=tuple(l[1:])[0]
print(final)
打印
{'sit': (1, 1), 'laboris': (2, 1), 'nisi': (2, 1), 'est': (4, 1), 'qui': (4, 1), 'cillum': (3, 1), 'voluptate': (3, 1), 'eu': (3, 1), 'irure': (3, 1), 'sunt': (4, 1), 'reprehenderit': (3, 1), 'nulla': (3, 1), 'sint': (4, 1), 'fugiat': (3, 1), 'dolore': (3, 1), 'enim': (2, 1), 'occaecat': (4, 1), 'tempor': (2, 1), 'commodo': (2, 1), 'non': (4, 1), 'minim': (2, 1), 'aute': (3, 1), 'ut': (2, 2), 'ex': (2, 1), 'deserunt': (4, 1), 'ea': (2, 1), 'eiusmod': (2, 1), 'culpa': (4, 1), 'labore': (2, 1), 'mollit': (4, 1), 'officia': (4, 1), 'cupidatat': (4, 1), 'adipiscing': (2, 1), 'amet': (1, 1), 'et': (2, 1), 'ad': (2, 1), 'consectetur': (2, 1), 'anim': (4, 1), 'magna': (2, 1), 'quis': (2, 1), 'ullamco': (2, 1), 'dolor': (3, 1), 'aliquip': (2, 1), 'velit': (3, 1), 'ipsum': (1, 1), 'incididunt': (2, 1), 'sed': (2, 1), 'id': (4, 1), 'esse': (3, 1), 'exercitation': (2, 1), 'nostrud': (2, 1)}