gpt4 book ai didi

python - 使用 zip 将元组按顺序附加到列表内的列表的更好方法

转载 作者:行者123 更新时间:2023-12-01 00:18:11 24 4
gpt4 key购买 nike

我想使用 zip 来组合结果,其中值应附加到相应的人员

假设我有一个包含多个列表的列表,如下所示:

列表:

[[('person_1', 6), ('person_2', 3), ('person_3', 7), ('person_4', 6), ('person_5', 12), ('person_6', 1), ('person_7', 1)], 
[('person_1', 3), ('person_2', 3), ('person_3', 0), ('person_4', 3), ('person_5', 1), ('person_6', 0), ('person_7', 0)],
[('person_1', 3), ('person_2', 4), ('person_3', 0), ('person_4', 3), ('person_5', 3), ('person_6', 0), ('person_7', 0)],
[('person_1', 1), ('person_2', 3), ('person_3', 0), ('person_4', 3), ('person_5', 0), ('person_6', 0), ('person_7', 0)],
[('person_1', 9), ('person_2', 2), ('person_3', 3), ('person_4', 6), ('person_5', 6), ('person_6', 0), ('person_7', 1)],
[('person_1', 0), ('person_2', 1), ('person_3', 0), ('person_4', 0), ('person_5', 1), ('person_6', 0), ('person_7', 0)],
[('person_1', 7), ('person_2', 2), ('person_3', 4), ('person_4', 6), ('person_5', 5), ('person_6', 0), ('person_7', 1)],
[('person_1', 7), ('person_2', 2), ('person_3', 4), ('person_4', 6), ('person_5', 5), ('person_6', 0), ('person_7', 1)]]

期望结果

[('person_1', 6, 3, 3, 1, 9, 0, 7, 7), ('person_2', 3, 3, 4, 3, 2, 1, 2, 2), ('person_3', 7, 0, 0, 0, 3, 0, 4, 4), ('person_4', 6, 3, 3, 3, 6, 0, 6, 6), ('person_5', 12, 1, 3, 0, 6, 1, 5, 5), ('person_6', 1, 0, 0, 0, 0, 0, 0, 0), ('person_7', 1, 0, 0, 0, 1, 0, 1, 1)]

这是我为实现目标所做的事情,但我想知道是否有更好的方法或切割器解决方案来做到这一点。任何建议表示赞赏

def merge(data):
data_sort = list()

for i in range(len(data[1:])):
x = data[1:][i]
data_list = list(zip(*x))[1]
data_sort.append(data_list)

data_result = list(zip(*data_sort))

return data_result

def main():

list_b = merge(list_a)
list_c = [(person, c_0, c_1, c_2, c_3, c_4, c_5, c_6, c_7) for ((person, c_0), c_1, c_2, c_3, c_4, c_5, c_6, c_7) in zip(list_a[0], list_b[0], list_c[1], list_c[2], list_c[3], list_c[4], list_c[5], list_c[6])]

if __name__ == '__main__':
main()

最佳答案

这是一种使用列表理解和列表/元组解包的方法*

例如:

data = [[('person_1', 6), ('person_2', 3), ('person_3', 7), ('person_4', 6), ('person_5', 12), ('person_6', 1), ('person_7', 1)], 
[('person_1', 3), ('person_2', 3), ('person_3', 0), ('person_4', 3), ('person_5', 1), ('person_6', 0), ('person_7', 0)],
[('person_1', 3), ('person_2', 4), ('person_3', 0), ('person_4', 3), ('person_5', 3), ('person_6', 0), ('person_7', 0)],
[('person_1', 1), ('person_2', 3), ('person_3', 0), ('person_4', 3), ('person_5', 0), ('person_6', 0), ('person_7', 0)],
[('person_1', 9), ('person_2', 2), ('person_3', 3), ('person_4', 6), ('person_5', 6), ('person_6', 0), ('person_7', 1)],
[('person_1', 0), ('person_2', 1), ('person_3', 0), ('person_4', 0), ('person_5', 1), ('person_6', 0), ('person_7', 0)],
[('person_1', 7), ('person_2', 2), ('person_3', 4), ('person_4', 6), ('person_5', 5), ('person_6', 0), ('person_7', 1)],
[('person_1', 7), ('person_2', 2), ('person_3', 4), ('person_4', 6), ('person_5', 5), ('person_6', 0), ('person_7', 1)]]


result = [[*i] + [j for _, j in values] for i, *values in zip(*data) ]
print(result)

输出:

[['person_1', 6, 3, 3, 1, 9, 0, 7, 7],
['person_2', 3, 3, 4, 3, 2, 1, 2, 2],
['person_3', 7, 0, 0, 0, 3, 0, 4, 4],
['person_4', 6, 3, 3, 3, 6, 0, 6, 6],
['person_5', 12, 1, 3, 0, 6, 1, 5, 5],
['person_6', 1, 0, 0, 0, 0, 0, 0, 0],
['person_7', 1, 0, 0, 0, 1, 0, 1, 1]]

关于python - 使用 zip 将元组按顺序附加到列表内的列表的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59152534/

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