gpt4 book ai didi

python - 如果第一个元素和另一个元素是相同的 Python,则将元组列表切片为列表列表

转载 作者:行者123 更新时间:2023-12-04 08:04:24 25 4
gpt4 key购买 nike

我有以下列表 A包括元组,我想切片 A进入列表列表,如 B 所示.逻辑是,如果元组的第一个和第四个元素重复,则将组打包为列表中的列表 A .

A = [(1, 'C-30219', 'C-30060', 'C-6235d935d39c258876476e35a7acfd69-1-1', 2),
(1, 'C-30060', 'C-30022', 'C-6235d935d39c258876476e35a7acfd69-1-1', 3),
(1, 'C-30022', 'C-30205', 'C-6235d935d39c258876476e35a7acfd69-1-1', 4),
(3, 'C-30248', 'C-30260', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 4),
(3, 'C-30260', 'C-30108', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 5),
(3, 'C-30108', 'C-30240', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 6),
(5, 'C-30269', 'C-30285', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 9),
(5, 'C-30285', 'C-30109', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 10),
(5, 'C-30109', 'C-30211', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 11),
(5, 'C-30211', 'C-30289', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 12),
(5, 'C-30072', 'C-30375', 'C-710c460e8dfc2b3a523e077b6c6bdb40-1-1', 15),
(5, 'C-30375', 'C-30095', 'C-710c460e8dfc2b3a523e077b6c6bdb40-1-1', 16)]
出去:
B = [[(1, 'C-30219', 'C-30060', 'C-6235d935d39c258876476e35a7acfd69-1-1', 2),
(1, 'C-30060', 'C-30022', 'C-6235d935d39c258876476e35a7acfd69-1-1', 3),
(1, 'C-30022', 'C-30205', 'C-6235d935d39c258876476e35a7acfd69-1-1', 4)],
[(3, 'C-30248', 'C-30260', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 4),
(3, 'C-30260', 'C-30108', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 5),
(3, 'C-30108', 'C-30240', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 6)],
[(5, 'C-30269', 'C-30285', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 9),
(5, 'C-30285', 'C-30109', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 10),
(5, 'C-30109', 'C-30211', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 11),
(5, 'C-30211', 'C-30289', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 12)],
[(5, 'C-30072', 'C-30375', 'C-710c460e8dfc2b3a523e077b6c6bdb40-1-1', 15),
(5, 'C-30375', 'C-30095', 'C-710c460e8dfc2b3a523e077b6c6bdb40-1-1', 16)]]
这是我的尝试,它在大量笨拙的巨著之后提供了所需的输出。我正在寻找一种更有效和 Pythonic 的方式来实现这一目标。
inter = list(set([(i[0],i[3]) for i in A]))
B = {o_t: [] for o_t in inter}
for i in range(1, len(A)):
if (A[i][0] == A[i-1][0]
and A[i][3] == A[i-1][3]):
B[A[i][0],A[i][3]].append(A[i])
B[A[i][0],A[i][3]].append(A[i-1])
B = {key: sorted(list(set(B[key])), key = lambda x: x[-1]) for key in B.keys()}
list(B.values())

最佳答案

完美任务 groupby来自 itertools

from itertools import groupby
A = [(1, 'C-30219', 'C-30060', 'C-6235d935d39c258876476e35a7acfd69-1-1', 2),
(1, 'C-30060', 'C-30022', 'C-6235d935d39c258876476e35a7acfd69-1-1', 3),
(1, 'C-30022', 'C-30205', 'C-6235d935d39c258876476e35a7acfd69-1-1', 4),
(3, 'C-30248', 'C-30260', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 4),
(3, 'C-30260', 'C-30108', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 5),
(3, 'C-30108', 'C-30240', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 6),
(5, 'C-30269', 'C-30285', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 9),
(5, 'C-30285', 'C-30109', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 10),
(5, 'C-30109', 'C-30211', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 11),
(5, 'C-30211', 'C-30289', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 12),
(5, 'C-30072', 'C-30375', 'C-710c460e8dfc2b3a523e077b6c6bdb40-1-1', 15),
(5, 'C-30375', 'C-30095', 'C-710c460e8dfc2b3a523e077b6c6bdb40-1-1', 16)]
B = [list(g) for _,g in groupby(A, key=lambda x: (x[0], x[3]))]

print(B)
输出
[[(1, 'C-30219', 'C-30060', 'C-6235d935d39c258876476e35a7acfd69-1-1', 2),
(1, 'C-30060', 'C-30022', 'C-6235d935d39c258876476e35a7acfd69-1-1', 3),
(1, 'C-30022', 'C-30205', 'C-6235d935d39c258876476e35a7acfd69-1-1', 4)],
[(3, 'C-30248', 'C-30260', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 4),
(3, 'C-30260', 'C-30108', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 5),
(3, 'C-30108', 'C-30240', 'C-ac19d0edcf4d4ebe071e8d43be1901e2-1-1', 6)],
[(5, 'C-30269', 'C-30285', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 9),
(5, 'C-30285', 'C-30109', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 10),
(5, 'C-30109', 'C-30211', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 11),
(5, 'C-30211', 'C-30289', 'C-d0d36bb9f2a7e248638cff9a04065977-1-1', 12)],
[(5, 'C-30072', 'C-30375', 'C-710c460e8dfc2b3a523e077b6c6bdb40-1-1', 15),
(5, 'C-30375', 'C-30095', 'C-710c460e8dfc2b3a523e077b6c6bdb40-1-1', 16)]]

注意 :我假设 A 按第一个和第四个元素排序。 groupby 将对列表进行分组 [1,1,1,2,2,1,3,3][(1,1,1), (2,2), (1), (3,3)] .它不会对所有 1's 进行分组

关于python - 如果第一个元素和另一个元素是相同的 Python,则将元组列表切片为列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66300868/

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