Python:嵌套循环 [英] Python: Nested Loop

查看:66
本文介绍了Python:嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下:

>>> 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?

推荐答案

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

这篇关于Python:嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆