如何迭代包含列表的元组列表并依次对元素进行分组?

huangapple go评论87阅读模式
英文:

how to iterate through a list of tuples with lists and group elements sequentially?

问题

  1. 我有这个列表
  2. lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
  3. 我试图将这些值按顺序分组在元组中有什么方法可以做到这一点
  4. 期望的输出是
  5. [(0,75), (75, 1), (1, 30), (1, 41), (41,49), (49,55), (2,28), (28, 53), (53, 45)]
  6. found = []
  7. for tup in edge_list:
  8. found.append((tup[0], tup[1][0]))
  9. found.append((tup[1][0], tup[1][1]))
  10. found.append((tup[1][1], tup[1][2]))
  11. print(found)
  12. 是否有更好/更简单的方法来迭代这个列表不管第二个元组的大小是多少
  13. [(0, [82, 70, 79, 77, 42]), (1, [40, 61, 58, 66, 69]), (2, [80, 30, 12, 77, 9])]
英文:

I have this list

  1. 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

  1. [(0,75), (75, 1), (1, 30), (1, 41), (41,49), (49,55), (2,28), (28, 53), (53, 45)]
  2. found = []
  3. for tup in edge_list:
  4. found.append((tup[0], tup[1][0]))
  5. found.append((tup[1][0], tup[1][1]))
  6. found.append((tup[1][1], tup[1][2]))
  7. print(found)

is there a better/easier approach to iterate over this no matter what the size of the second tuple looks like

  1. [(0, [82, 70, 79, 77, 42]), (1, [40, 61, 58, 66, 69]), (2, [80, 30, 12, 77, 9])]

答案1

得分: 1

通过*运算符展开lis中的元素是一个有用的技巧:

  1. lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
  2. [(a, *b) for a, b in lis]
  3. [(0, 75, 1, 30), (1, 41, 49, 55), (2, 28, 53, 45)]

给定任何序列,你可以通过将其与自身的偏移版本进行zip处理来产生你想要的配对:

  1. list(zip(s, s[1:]))
  2. [(1, 2), (2, 3), (3, 4)]

结合这两种技巧(即取出lis中的序列,然后将它们转换为配对),可以进行如下操作:

  1. [list(zip((a, *b), b)) for a, b in lis]
  2. [[(0, 75), (75, 1), (1, 30)], [(1, 41), (41, 49), (49, 55)], [(2, 28), (28, 53), (53, 45)]]

为了将它们全部放入一个配对的列表中,可以使用嵌套的列表推导式:

  1. [z for a, b in lis for z in zip((a, *b), b)]
  2. [(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:

  1. >>> lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
  2. >>> [(a, *b) for a, b in lis]
  3. [(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 zipping it with an offset version of itself:

  1. >>> list(zip(s, s[1:]))
  2. [(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:

  1. >>> [list(zip((a, *b), b)) for a, b in lis]
  2. [[(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:

  1. >>> [z for a, b in lis for z in zip((a, *b), b)]
  2. [(0, 75), (75, 1), (1, 30), (1, 41), (41, 49), (49, 55), (2, 28), (28, 53), (53, 45)]

答案2

得分: 1

  1. 使用列表解包和 zip 方法
  2. lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
  3. result = []
  4. for a, b in lis:
  5. res = [a, *b]
  6. for i, j in zip(res, res[1:]):
  7. result.append((i,j))
  8. result
  9. # 输出如下:
  10. [(0, 75),
  11. (75, 1),
  12. (1, 30),
  13. (1, 41),
  14. (41, 49),
  15. (49, 55),
  16. (2, 28),
  17. (28, 53),
  18. (53, 45)]
英文:

using list unpacking and zip method

  1. In [49]: lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
  2. In [50]: result = []
  3. ...: for a, b in lis:
  4. ...: res = [a, *b]
  5. ...: for i, j in zip(res, res[1:]):
  6. ...: result.append((i,j))
  7. ...:
  8. In [51]: result
  9. Out[51]:
  10. [(0, 75),
  11. (75, 1),
  12. (1, 30),
  13. (1, 41),
  14. (41, 49),
  15. (49, 55),
  16. (2, 28),
  17. (28, 53),
  18. (53, 45)]

答案3

得分: 0

以下是代码部分的中文翻译:

  1. import itertools
  2. lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
  3. found = [*itertools.chain(*(itertools.pairwise(a) for *a, a[1:] in lis))]
  4. print(found)

在线尝试此代码!

英文:

Having fun with itertools...

  1. from itertools import pairwise, chain
  2. lis = [(0, [75, 1, 30]), (1, [41, 49, 55]), (2, [28, 53, 45])]
  3. found = [*chain(*(pairwise(a) for *a, a[1:] in lis))]
  4. print(found)

Attempt This Online!

huangapple
  • 本文由 发表于 2023年2月18日 08:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定