使用zip将值从列表追加到另一个

问题描述 投票:0回答:3

我有以下两个列表

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的其他数字

欲望的结果是这样的字典格式的东西:

{'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)

[试图了解我在哪里做错了,还有什么其他好的方法可以做到这一点。任何帮助,将不胜感激

python list append
3个回答
2
投票
您总是将list_2传递给zip。因此,在每个循环中,将需要前4个元素到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
投票
尝试事先将字典理解与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')]}


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

© www.soinside.com 2019 - 2024. All rights reserved.