gpt4 book ai didi

python - python 3中列表的合并列表

转载 作者:行者123 更新时间:2023-12-01 02:34:56 25 4
gpt4 key购买 nike

我有这 2 个列表:

list1= [['user1', 186, 'Feb 2017, Apr 2017', 550, 555], ['user2', 282, 'Mai 2017', 0, 3579], ['user3', 281, 'Mai 2017', 10, 60]]

list2= [['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 0, 740],['user2', 282, 'Feb 2017', 0, 1000], ['user4', 288, 'Feb 2017', 60, 10]]

我希望输出这个:

desiredlist =[['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 550, 740], ['user2', 282, 'Feb 2017', 0, 1000], ['user3', 281, 'Mai 2017', 10, 0], ['user4', 288, 'Feb 2017', 60, 10]]

为此我有这个功能:

def mergesafirmacheta(list1,list2):
desiredlist = []
for id, n1, dates, n2, n3 in list1:
counter = 0
for list_2 in list2:
if n1 == list_2[1]:
desiredlist.append(list_2[:3] + [n2, list_2[4]])
else:
counter += 1
if counter == len(list2):
desiredlist.append([id, n1, dates, n2, 0])
print (desiredlist)

但这只会输出:

desiredlist =[['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 550, 740], ['user2', 282, 'Feb 2017', 0, 1000], ['user3', 281, 'Mai 2017', 10, 0]]

^^ user4 丢失(检查我想要的输出),我可以从我的函数中看出原因,我尝试在第一个 is 语句之后添加此内容:

elif list_2[1] != n1:
desiredlist.append(list_2)

但这行不通,这是计算所需列表的规则:

list2[1] == list1[1] (示例 186 == 186)附加到我的 desiredlist list2[0], list2[1 ]、list2[2]、list1[3]、list2[4],如果您的 list1[1] 不在 list2 列表中,则更改追加到 desiredlist list1[0], list1[1], list1[2], list1[3], 0, 并且如果 list2[1] 不在list1 列表附加到 desiredlist list2[0]、list2[1]、list2[2]、list2[3]、list2[4]。这有点难以放松,但我认为检查我想要的输出和我现在实际输出的内容要容易得多。

最佳答案

我通过更改格式成功实现了您的预期。我使用 user 键将您的所有子列表转换为 dict

因为合并字典更容易,并且子列表中 user 的顺序并不重要。

最后一步是迭代 list1list2 的合并 dict 并执行特殊操作。据我了解,就是获取 list1 的前一个数字并将其与 list2 合并。然后重新创建所需的子列表。

from itertools import chain
from collections import defaultdict

list1 = [['user1', 186, 'Feb 2017, Apr 2017', 550, 555], ['user2', 282, 'Mai 2017', 0, 3579], ['user3', 281, 'Mai 2017', 10, 60]]
list2 = [['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 0, 740],['user2', 282, 'Feb 2017', 0, 1000], ['user4', 288, 'Feb 2017', 60, 10]]

# Transform list to dict with key as 'userN'
def to_dict(lst): return {x[0]: x[1:] for x in lst}

# Now create a dict that combined list of user1..N+1
tmp_dict = defaultdict(list)
for k, v in chain(to_dict(list1).items(), to_dict(list2).items()):
tmp_dict[k].append(v)

desired_output = []
for k, v in tmp_dict.items():
if len(v) == 2:
v[1][-2] = v[0][-2] # Take the before last of list1 to remplace with before last one of list2
desired_output.append([k] + v[1])
else:
desired_output.append([k] + v[0])

print(desired_output)

输出:

[['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 550, 740], ['user2', 282, 'Feb 2017', 0, 1000], ['user3', 281, 'Mai 2017', 10, 60], ['user4', 288, 'Feb 2017', 60, 10]]

编辑

看来我犯了一个错误,你的list1必须检查list2的所有内容,在这种情况下你应该做一个dict首先列出 list2,然后应用您的特定条件。例如:

from itertools import chain

list1 = [['user1', 186, 'Feb 2017, Apr 2017', 550, 555], ['user2', 282, 'Mai 2017', 0, 3579], ['user3', 281, 'Mai 2017', 10, 60]]
list2 = [['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 0, 740],['user2', 282, 'Feb 2017', 0, 1000], ['user4', 288, 'Feb 2017', 60, 10]]

# Transform list to dict with key as 'userN'
def to_dict(lst): return {x[0]: x[1:] for x in lst}

# First, transfrom list2 to dict
list2_dict = {}
for k, v in to_dict(list2).items():
list2_dict[k] = v

# Then iterate on list1 to compare
desired_output = []
for k, v in to_dict(list1).items():
if k in list2_dict: # key of list1 exists in list2
list2_dict[k][-2] = v[-2] # replace value
desired_output.append([k] + list2_dict[k]) # Then add list2
list2_dict.pop(k) # And remove it from old dict
else: # list1 does not exists in list2
v[-1] = 0 # Set last element to zero
desired_output.append([k] + v)

for k, v in list2_dict.items(): # Now add elements present only in list2
desired_output.append([k] + v)

print(desired_output)

输出:

[['user1', 186, 'Feb 2017, Mar 2017, Mai 2017', 550, 740], ['user2', 282, 'Feb 2017', 0, 1000], ['user3', 281, 'Mai 2017', 10, 0], ['user4', 288, 'Feb 2017', 60, 10]]

注意:我们可以去掉 defaultdict,因为同一个键不会被添加两次。

关于python - python 3中列表的合并列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46362972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com