Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表列表转换为具有冗余键的字典?_Python_List_Dictionary - Fatal编程技术网

Python 如何将列表列表转换为具有冗余键的字典?

Python 如何将列表列表转换为具有冗余键的字典?,python,list,dictionary,Python,List,Dictionary,我有一个列表,称为我的列表: 我试过: d = {} for item in all_lists: d[item[0]] = item[1:] print (d) 但这会覆盖密钥,而不是更新该值。例如,dolor变成:{'dolor':[3,1],而不是所需的目标:{'dolor':3,1,1,等等 理想情况下,字典形状不包括元组列表作为值,但如果需要的话 如何将列表列表转换为我想要的格式的dict 我已经观察到了,但这给我带来了不正确的答案。使用defaultdict功能: 未测

我有一个列表,称为我的列表:

我试过:

d = {}

for item in all_lists:
    d[item[0]] = item[1:]

print (d)
但这会覆盖密钥,而不是更新该值。例如,dolor变成:{'dolor':[3,1],而不是所需的目标:{'dolor':3,1,1,等等

理想情况下,字典形状不包括元组列表作为值,但如果需要的话

如何将列表列表转换为我想要的格式的dict


我已经观察到了,但这给我带来了不正确的答案。

使用defaultdict功能:

未测试代码

d = defaultdict(list)

for item in all_lists:
    d[item[0]].append(item[1:])

print (d)

其中一种方法是,如果d缺少此键且值为空列表[],则添加新的键值对。然后将新值附加到该列表

Python必须这样做

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)]
}

这很烦人,但您可以通过快速调用构造函数将列表更改为元组

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)}
如果键已排序,则可以使用以下选项:

import itertools as it
result = {k: [x[1] for x in v] for k, v in it.groupby(test, key=lambda x: x[0])}

对于示例dolor I posted,可能的重复操作失败。dict不能包含重复的键,因为字符串将具有相同的哈希。如果您希望通过其他方式为dict设置键,则“确定”,但尝试使用相同的键写入第二个值将始终失败。除非它像其他人一样包含为元组元组或元组列表已发布。它将每个元组包装到额外列表中。例如,{'sit':[[1,1],[2,2]],'laboris':[[2,1]}如何修复@ovgolovin@JerryM.appenditem[1]而不是appenditem[1:].如果不是一直分类怎么办?在这种情况下是的,但我认为我们是lucky@JerryM.您总是可以预先使用sorted,但对于具有许多不同键的大型列表,这可能会影响性能,最好使用defaultdict。
import itertools as it
result = {k: [x[1] for x in v] for k, v in it.groupby(test, key=lambda x: x[0])}