Python:嵌套循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1821471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:08:29  来源:igfitidea点击:

Python: Nested Loop

pythonloopsnested

提问by mRt

Consider this:

考虑一下:

>>> a = [("one","two"), ("bad","good")]

>>> for i in a:
...     for x in i:
...         print x
... 
one
two
bad
good

How can I write this code, but using a syntax like:

我如何编写此代码,但使用如下语法:

for i in a:
    print [x for x in i]

Obviously, This does not work, it prints:

显然,这不起作用,它打印:

['one', 'two']
['bad', 'good']

I want the same output. Can it be done?

我想要相同的输出。可以做到吗?

回答by Benjamin Pollack

List comprehensions and generators are only designed to be used as expressions, while printing is a statement. While you can effect what you're trying to do by doing

列表推导式和生成器仅被设计为用作表达式,而打印则是一个语句。虽然你可以通过做来影响你想做的事

from __future__ import print_function
for x in a:
    [print(each) for each in x]

doing so is amazingly unpythonic, and results in the generation of a list that you don't actually need. The best thing you could do would simply be to write the nested forloops in your original example.

这样做非常不符合 Python 标准,并且会生成您实际上并不需要的列表。您能做的最好的事情就是for在原始示例中编写嵌套循环。

回答by tgray

Given your example you could do something like this:

鉴于您的示例,您可以执行以下操作:

a = [("one","two"), ("bad","good")]

for x in sum(map(list, a), []):
    print x

This can, however, become quite slow once the list gets big.

但是,一旦列表变大,这可能会变得非常缓慢。

The better way to do it would be like Tim Pietzckersuggested:

更好的方法是像Tim Pietzcker建议的那样:

from itertools import chain

for x in chain(*a):
    print x

Using the star notation, *a, allows you to have n tuples in your list.

使用星号, *a,允许您在列表中包含 n 个元组。

回答by John La Rooy

>>> a = [("one","two"), ("bad","good")]
>>> print "\n".join(j for i in a for j in i)
one
two
bad
good



>>> for i in a:
...  print "\n".join(i)
... 
one
two
bad
good

回答by u0b34a0f6ae

The print function really is superior, but here is a much more pythonic suggestion inspired by Benjamin Pollack's answer:

打印功能确实更胜一筹,但这里有一个受本杰明波拉克回答启发的更加 pythonic 的建议:

from __future__ import print_function
for x in a:
    print(*x, sep="\n")

Simply use *to unpack the list x as arguments to the function, and use newline separators.

只需用于*将列表 x 解包为函数的参数,并使用换行符。

回答by Tim Pietzcker

import itertools
for item in itertools.chain(("one","two"), ("bad","good")):
    print item

will produce the desired output with just one forloop.

只需一个for循环即可产生所需的输出。

回答by Ofri Raviv

This code is straightforward and simpler than other solutions here:

此代码比此处的其他解决方案简单明了:

for i in a:
    print '\n'.join([x for x in i])

回答by notnoop

You'll need to define your own print method (or import __future__.print_function)

您需要定义自己的打印方法(或 import __future__.print_function

def pp(x): print x

for i in a:
  _ = [pp(x) for x in i]

Note the _is used to indicate that the returned list is to be ignored.

注意_用于指示返回的列表将被忽略。

回答by Lucas Gabriel Sánchez

Not the best, but:

不是最好的,但是:

for i in a:
    some_function([x for x in i])

def some_function(args):
    for o in args:
        print o