英文:
how to iterate through a list of tuples with lists and group elements sequentially?
问题
我有这个列表
lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
我试图将这些值按顺序分组在元组中。有什么方法可以做到这一点?
期望的输出是
[(0,75), (75, 1), (1, 30), (1, 41), (41,49), (49,55), (2,28), (28, 53), (53, 45)]
found = []
for tup in edge_list:
found.append((tup[0], tup[1][0]))
found.append((tup[1][0], tup[1][1]))
found.append((tup[1][1], tup[1][2]))
print(found)
是否有更好/更简单的方法来迭代这个列表,不管第二个元组的大小是多少
[(0, [82, 70, 79, 77, 42]), (1, [40, 61, 58, 66, 69]), (2, [80, 30, 12, 77, 9])]
英文:
I have this list
lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
and I'm trying to group these values sequentially together in tuples. What would be a way to do this?
Expected output is
[(0,75), (75, 1), (1, 30), (1, 41), (41,49), (49,55), (2,28), (28, 53), (53, 45)]
found = []
for tup in edge_list:
found.append((tup[0], tup[1][0]))
found.append((tup[1][0], tup[1][1]))
found.append((tup[1][1], tup[1][2]))
print(found)
is there a better/easier approach to iterate over this no matter what the size of the second tuple looks like
[(0, [82, 70, 79, 77, 42]), (1, [40, 61, 58, 66, 69]), (2, [80, 30, 12, 77, 9])]
答案1
得分: 1
通过*
运算符展开lis
中的元素是一个有用的技巧:
lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
[(a, *b) for a, b in lis]
[(0, 75, 1, 30), (1, 41, 49, 55), (2, 28, 53, 45)]
给定任何序列,你可以通过将其与自身的偏移版本进行zip
处理来产生你想要的配对:
list(zip(s, s[1:]))
[(1, 2), (2, 3), (3, 4)]
结合这两种技巧(即取出lis
中的序列,然后将它们转换为配对),可以进行如下操作:
[list(zip((a, *b), b)) for a, b in lis]
[[(0, 75), (75, 1), (1, 30)], [(1, 41), (41, 49), (49, 55)], [(2, 28), (28, 53), (53, 45)]]
为了将它们全部放入一个配对的列表中,可以使用嵌套的列表推导式:
[z for a, b in lis for z in zip((a, *b), b)]
[(0, 75), (75, 1), (1, 30), (1, 41), (41, 49), (49, 55), (2, 28), (28, 53), (53, 45)]
英文:
A useful trick is to flatten out the elements of lis
via the *
operator:
>>> lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
>>> [(a, *b) for a, b in lis]
[(0, 75, 1, 30), (1, 41, 49, 55), (2, 28, 53, 45)]
Given any sequence, you can produce the kind of pairing you're looking for by zip
ping it with an offset version of itself:
>>> list(zip(s, s[1:]))
[(1, 2), (2, 3), (3, 4)]
Combining those two techniques (i.e. taking the sequences within lis
and then converting them into pairs), you can do:
>>> [list(zip((a, *b), b)) for a, b in lis]
[[(0, 75), (75, 1), (1, 30)], [(1, 41), (41, 49), (49, 55)], [(2, 28), (28, 53), (53, 45)]]
and to get them all into a single list of pairs, we can do a nested list comprehension:
>>> [z for a, b in lis for z in zip((a, *b), b)]
[(0, 75), (75, 1), (1, 30), (1, 41), (41, 49), (49, 55), (2, 28), (28, 53), (53, 45)]
答案2
得分: 1
使用列表解包和 zip 方法
lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
result = []
for a, b in lis:
res = [a, *b]
for i, j in zip(res, res[1:]):
result.append((i,j))
result
# 输出如下:
[(0, 75),
(75, 1),
(1, 30),
(1, 41),
(41, 49),
(49, 55),
(2, 28),
(28, 53),
(53, 45)]
英文:
using list unpacking and zip method
In [49]: lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
In [50]: result = []
...: for a, b in lis:
...: res = [a, *b]
...: for i, j in zip(res, res[1:]):
...: result.append((i,j))
...:
In [51]: result
Out[51]:
[(0, 75),
(75, 1),
(1, 30),
(1, 41),
(41, 49),
(49, 55),
(2, 28),
(28, 53),
(53, 45)]
答案3
得分: 0
以下是代码部分的中文翻译:
import itertools
lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
found = [*itertools.chain(*(itertools.pairwise(a) for *a, a[1:] in lis))]
print(found)
英文:
Having fun with itertools...
from itertools import pairwise, chain
lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
found = [*chain(*(pairwise(a) for *a, a[1:] in lis))]
print(found)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论