我有以下两个列表
list_1
[('Person A', [(6, "example.com/h"), (1, "example.com/xqz"), (7, "example.com/abc"), (9, "example.com/efg")]),
('Person B', [(4, "example.com/a"), (5, "example.com/rrr"), (1, "example.com/ddd"), (4, "example.com/nnn")]),
('Person C', [(3, "example.com/g"), (1, "example.com/qqq"), (7, "example.com/fff"), (3, "example.com/vvv")])]
list_2
['2', '6', '2', '23', '2', '6', '9', '25', '2', '5', '7', '19']
list_1包含属于一个人的数据计数以及数据来自的超链接。 list_2包含我想分别添加到list_1的其他数字
欲望结果是这样的dict格式:
{'Person A': [((6, "example.com/h"), '2'), ((1, "example.com/xqz"), '6'), ((7, "example.com/abc"), '2'), ((9, "example.com/efg"), '23')],
'Person B': [((4, "example.com/a"), '2'), ((5, "example.com/rrr"), '6'), ((1, "example.com/ddd"), '9'), ((4, "example.com/nnn"), '25')],
'Person C': [((3, "example.com/g"), '2'), ((1, "example.com/qqq"), '5'), ((7, "example.com/fff"), '7'), ((3, "example.com/vvv"), '19')]}
我最初的想法只是使用zip获得我想要的结果,但是我得到的结果不正确:
{'Person A': [((6, "example.com/h"), '2'), ((1, "example.com/xqz"), '6'), ((7, "example.com/abc"), '2'), ((9, "example.com/efg"), '23')],
'Person B': [((4, "example.com/a"), '2'), ((5, "example.com/rrr"), '6'), ((1, "example.com/ddd"), '2'), ((4, "example.com/nnn"), '23')],
'Person C': [((3, "example.com/g"), '2'), ((1, "example.com/qqq"), '6'), ((7, "example.com/fff"), '2'), ((3, "https://xxx./vvv"), '23')]}
sample_dict = dict()
for i in list_1:
result = list(zip(i[1], list_2))
sample_dict[i[0]] = result
print(sample_dict)
试图了解我在哪里做错了,还有什么其他好的方法可以做到这一点。任何帮助将不胜感激
答案 0 :(得分:4)
尝试预先将字典理解与iter
一起使用:
it = iter(list_2)
print({k:[(i, next(it)) for i in v] for k, v in list_1})
输出:
{'Person A': [((6, 'example.com/h'), '2'), ((1, 'example.com/xqz'), '6'), ((7, 'example.com/abc'), '2'), ((9, 'example.com/efg'), '23')], 'Person B': [((4, 'example.com/a'), '2'), ((5, 'example.com/rrr'), '6'), ((1, 'example.com/ddd'), '9'), ((4, 'example.com/nnn'), '25')], 'Person C': [((3, 'example.com/g'), '2'), ((1, 'example.com/qqq'), '5'), ((7, 'example.com/fff'), '7'), ((3, 'example.com/vvv'), '19')]}
答案 1 :(得分:2)
您始终为list_2
传递zip
。因此,在每个循环中,将前四个元素移至zip
。
需要从list_2中删除已压缩的元素,以进行下一次迭代。
您可以尝试如下
sample_dict = dict()
for i in list_1:
result = [c for c in zip(i[1], list_2)]
sample_dict[i[0]] = result
list_2 = list_2[len(i[1]):]
sample_dict
它为您提供以下输出。
{'Person A': [((6, 'example.com/h'), '2'),
((1, 'example.com/xqz'), '6'),
((7, 'example.com/abc'), '2'),
((9, 'example.com/efg'), '23')],
'Person B': [((4, 'example.com/a'), '2'),
((5, 'example.com/rrr'), '6'),
((1, 'example.com/ddd'), '9'),
((4, 'example.com/nnn'), '25')],
'Person C': [((3, 'example.com/g'), '2'),
((1, 'example.com/qqq'), '5'),
((7, 'example.com/fff'), '7'),
((3, 'example.com/vvv'), '19')]}
答案 2 :(得分:0)
您可以像这样将list2
分为4组
>>> updates = [list2[i:i+4] for i in range(0,len(list2),4)]
>>> updates
[['2', '6', '2', '23'], ['2', '6', '9', '25'], ['2', '5', '7', '19']]
然后创建一个dict
>>> output = {}
>>> for a,b in zip(list1, updates): )])
... output[a[0]] = [(x,y) for x,y in zip(a[1],b)]
...
>>> output
{'Person A': [((6, 'example.com/h'), '2'),
((1, 'example.com/xqz'), '6'),
((7,'example.com/abc'), '2'),
((9, 'example.com/efg'), '23')],
'Person B': [((4, 'example.com/a'), '2'),
((5, 'example.com/rrr'), '6'),
((1, 'example.com/ddd'), '9'),
((4, 'example.com/nnn'), '25')],
'Person C': [((3, 'example.com/g'), '2'),
((1, 'example.com/qqq'), '5'),
((7, 'example.com/fff'), '7'),
((3, 'example.com/vvv'), '19')]}