递归列表理解 [英] Recursive list comprehension

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

问题描述

大家好。


我的列表看起来像[[''N'',''F''],[''E''],[' 'D'']]

我试着让它变平:[''N'',''F'',''E'','D'']

如何通过列表理解来达到这样的效果?

两个周期完成了这项工作,但这种方式并不像pythonic ..

我试过

print [x for x in y in c_vars]

并得到NameError:name''y''未定义。


-

Timothy Babytch

解决方案

Timothy Babytch写道:
我的列表看起来像[[''N'',''F''],[''E''] ,[''D'']]
我试着把它弄平:[''N'',''F'',''E'','D'']

如何通过列表理解来达到这样的效果?
两个周期完成了这项工作,但那样做了方式看起来并不像pythonic ..

我试过打印[x for x in y in c_vars]
并得到NameError:name''y''未定义。




for表达式的顺序与嵌套循环的顺序相同:

items = [[''N'',''F''],[''E''],[''D'']]
[y for x中的y项目中的x]
[''N'',''F'',''E'',''D'']


我仍然会更喜欢for循环,因为它可以让你免于迭代
python中的子列表项:

data = []
for sub in [['' N'',''F''],[''E''],[''D'']]:
.... data.extend(sub)

....数据



[''N'',''F'',''E'','D'']


彼得


Peter Otten写道:

for表达式的顺序与嵌套循环的顺序相同:

items = [[''N '',''F''],[''E''],[''D'']]
[y代表x中y项目的x]

我仍然更喜欢for循环,因为它可以让你免于迭代在python中的子列表项:



data = []
for sub in [[''N'',''F''],[''E''],[''D'']]:



.. .data.extend(sub)
...




谢谢。这两个提示都很有帮助。


-

Timothy Babytch


2004年12月6日星期一09 :26,Timothy Babytch写道:

大家好。

我的列表看起来像[[''N'',''F''],[' 'E'',[''D'']]
我试着把它弄平:[''N',''F'',''E'','D'' ]

如何通过列表理解来实现这样的效果?
两个周期完成了这项工作,但那种方式看起来并不像pythonic ..

我试过
print [x for x in y for y in c_vars]
并得到NameError:name''y''未定义。

-
Timothy Babytch







我认为你用这样的发电机做到这一点:


def flatten(嵌套):

表示嵌套子列表:

表示子列表中的元素:

yield element


n = [[''N'',''F''],[''E''] ,[''D'']]

输出= []


for flatten(n):

输出.append(价值)


打印输出


祝圣诞快乐


Peter Nuttall


Hi all.

I have a list that looks like [[''N'', ''F''], [''E''], [''D'']]
I try to make it flat one: [''N'', ''F'', ''E'', ''D'']

How can I archieve such an effect with list comprehension?
Two cycles did the job, but that way did not look pythonic..

I tried
print [x for x in y for y in c_vars]
and got NameError: name ''y'' is not defined.

--
Timothy Babytch

解决方案

Timothy Babytch wrote:

Hi all.

I have a list that looks like [[''N'', ''F''], [''E''], [''D'']]
I try to make it flat one: [''N'', ''F'', ''E'', ''D'']

How can I archieve such an effect with list comprehension?
Two cycles did the job, but that way did not look pythonic..

I tried
print [x for x in y for y in c_vars]
and got NameError: name ''y'' is not defined.



The order of the for expressions is as it would be for nested loops:

items = [[''N'', ''F''], [''E''], [''D'']]
[y for x in items for y in x] [''N'', ''F'', ''E'', ''D'']

I would still prefer a for loop because it spares you from iterating over
the sublist items in python:
data = []
for sub in [[''N'', ''F''], [''E''], [''D'']]: .... data.extend(sub)
.... data


[''N'', ''F'', ''E'', ''D'']

Peter


Peter Otten wrote:

The order of the for expressions is as it would be for nested loops:

items = [[''N'', ''F''], [''E''], [''D'']]
[y for x in items for y in x]

I would still prefer a for loop because it spares you from iterating over
the sublist items in python:


data = []
for sub in [[''N'', ''F''], [''E''], [''D'']]:



... data.extend(sub)
...



Thanks. Both tips were helpful.

--
Timothy Babytch


On Monday 06 Dec 2004 09:26, Timothy Babytch wrote:

Hi all.

I have a list that looks like [[''N'', ''F''], [''E''], [''D'']]
I try to make it flat one: [''N'', ''F'', ''E'', ''D'']

How can I archieve such an effect with list comprehension?
Two cycles did the job, but that way did not look pythonic..

I tried
print [x for x in y for y in c_vars]
and got NameError: name ''y'' is not defined.

--
Timothy Babytch



Hi,

I think you do it with a generator like this:

def flatten(nested):
for sublist in nested:
for element in sublist:
yield element

n=[[''N'', ''F''], [''E''], [''D'']]
output=[]

for value in flatten(n):
output.append(value)

print output

Have a merry Christmas

Peter Nuttall


这篇关于递归列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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