简体   繁体   English

如何在元组列表中组合元组

[英]How to combine tuples in list of tuples

Combining tuples in list of tuples在元组列表中组合元组

test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]

How to get this output:如何获得此输出:

[(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

Just to go into a bit more detail about how to do this with list comprehensions and explain what they are and how they work...只是为了更详细地了解如何使用列表推导式执行此操作并解释它们是什么以及它们如何工作......

To begin with, here's a fairly long-winded way of achieving what you want:首先,这里有一个相当冗长的方式来实现你想要的:

test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]

result = []  # set up empty list to hold the result
for group in test_list:  # loop through each 'group' in your list
    (numbers, text) = group  # unpack into the list of numbers and the text string
    for n in numbers:  # loop through the numbers
        result.append((n, text))  # add the (number, text) tuple to the result list

print(result)
# [(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'), (3, 'cs')]

So we've achieved the result using two for loops, one nested inside the other.所以我们使用两个for循环实现了结果,一个嵌套在另一个里面。

But there's a really neat Python construct called a list comprehension which lets you do this kind of loop in just one line.但是有一个非常简洁的 Python 构造,称为列表推导式,它可以让您在一行中完成这种循环。

Using an example with just a single loop:使用只有一个循环的示例:

numbers = [1, 2, 3]

doubles = []
for n in numbers:
    doubles.append(n * 2)

print(doubles)
# [2, 4, 6]

We can re-write this as the following list comprehension:我们可以将其重写为以下列表推导式:

numbers = [1, 2, 3]

doubles = [n * 2 for n in numbers]

print(doubles)
# [2, 4, 6]

A list comprehension is of the form:列表理解的形式如下:

result = [<expression> for item in iterable]

which is equivalent to:这相当于:

result = []
for item in iterable:
    result.append(<expression>)

where <expression> is something involving item .其中<expression>是涉及item东西。

You can also nest list comprehensions like you can nest for loops.您还可以像嵌套for循环一样嵌套列表推导式。 Going back to our original problem, we need to first change it so that we 'unpack' group into numbers and text directly when we set up the for loop:回到我们原来的问题,我们需要把它使我们“解压”第一变groupnumberstext直接当我们建立for循环:

result = []
for (numbers, text) in test_list:
    for n in numbers:
        result.append((n, text))

Now imagine dragging the for loops off to the right until we can line them all up:现在想象一下将for循环拖到右边,直到我们可以将它们全部排列起来:

result = []
for (numbers, text) in test_list:
                                   for n in numbers:
                                                      result.append((n, text))

and then put the expression (ie (n, text) ) at the left:然后将表达式(即(n, text) )放在左侧:

result = [(n, text) for (numbers, text) in test_list for n in numbers]

List comprehensions may seem strange at first (especially if you're jumping straight into a double list comprehension!), but one you've got your head around how they work, they are really neat and can be very powerful!列表理解一开始可能看起来很奇怪(尤其是当您直接进入双重列表理解时!),但是您已经了解了它们的工作原理,它们真的很简洁并且非常强大! There are also similar set comprehensions and dictionary comprehensions.还有类似的集合推导式和字典推导式。 Read more here: https://dbader.org/blog/list-dict-set-comprehensions-in-python在此处阅读更多信息: https : //dbader.org/blog/list-dict-set-comprehensions-in-python

You can use nested list comprehensions:您可以使用嵌套列表推导式:

test_list = [([1, 2, 3], 'gfg'), ([5, 4, 3], 'cs')]

result = [(z, y) for x, y in test_list for z in x]
#          z = numbers in the lists inside the tuples
#          x = the lists inside the tuples
#          y = the strings inside the tuples

print(result)

Output:输出:

[(1, 'gfg'), (2, 'gfg'), (3, 'gfg'), (5, 'cs'), (4, 'cs'),(3, 'cs')]

result = [(z, y) for x, y in test_list for z in x] is the list comprehension version for: result = [(z, y) for x, y in test_list for z in x]是列表理解版本:

result = []
for x, y in test_list:
    for z in x:
        result.append((z,y))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM