将循环转换为列表理解 [英] convert loop to list comprehension

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

问题描述

请帮助我的可怜的大脑:)每次我尝试做一个清单

理解我发现我只是不理解...


无论如何,我有以下代码:


seq = [2,3,1,9]

tmp = []

for a range(len(seq)):

tmp.extend([a] * seq [a])


哪个正确返回:


[0,0,1,1,1,2,3,3,3,3,3,3,3,3,3]


问题是,我可以将其作为列表理解吗?


谢谢!

解决方案

" bv ** @ xplornet.com" < bv ** @ xplornet.comwrites:


seq = [2,3,1,9]

tmp = []

的范围内(len(seq)):

tmp.extend([a] * seq [a])


正确返回:


[0,0,1,1,1,2,3,3,3,3,3,3,3,3,3]


问题是,我可以将此作为列表理解吗?



导入运算符

x = reduce(operator.add,([i] * a代表i,a代表枚举(seq)), [])


Paul Rubin< http://ph****@NOSPAM.invalidwrites:


问题是,我可以将此作为列表理解吗?



导入运算符

x = reduce(operator.add,([i] * a表示i,a表示枚举(seq)), [])



在迭代精神中可能更多:


import itertools

seq = [2,3,1,9]

x = itertools.chain(*([i] * a代表i,a代表枚举(seq)))


bv**@xplornet.com 在新闻中写道:1157758817.446690.105620

@ i42g2000cwa。 com.lang.python中的googlegroups.com:


请帮助我的大脑:)每次我尝试做一个清单

理解我发现我只是不理解......


无论如何,我有以下代码:


seq = [2 ,3,1,9]

tmp = []

的范围内(len(seq)):

tmp.extend( [a] * seq [a])


正确返回:


[0,0,1,1,1,2, 3,3,3,3,3,3,3,3,3]


问题是,我可以将其作为列表理解吗?



很明显我可以,虽然它确实需要我2个:


>> seq = [2,3,1,9]
sum([[a] * seq [a] for a range(len(seq))])



Traceback(最近一次调用最后一次):

文件"< pyshell#1>",第1行,在-toplevel中

总和([[a] * seq [a]为范围内(len (seq))])

TypeError:不支持的操作数类型+:''int''和''list''


>> sum([[a] * seq [a]为范围内(len(seq))],[])



[0,0,1,1,1,2,3,3,3,3,3,3,3,3 ,3]


>>>



但这只是我炫耀一个肮脏的程序员我是什么。


我的第三次尝试如下:


>> [x for a in in range( len(seq))for [a] * seq [a]]



Traceback(最近一次调用最后一次):

文件"< pyshell#3>",第1行,在-toplevel-

[x为a in a范围(len(seq))x在[a] * seq [a]]

TypeError:迭代非序列


啊cut-n-paste的危险(和我的一样)一个奇怪的错误),

但是我的第四次尝试是愚蠢的(如果这是一个双关语我做了道歉)

这个:


>> [x for a range(len(seq))for x in [a] * seq [a]]



[0,0,1,1,1,2,3,3,3,3,3,3,3,3,3]
< blockquote class =post_quotes>


>>>



这可能是你所期望的。


注意展开的版本去了:


>> tmp = []
适用于范围内( len(seq)):



for [a] * seq [a]:

tmp.append(x)


>> tmp



[0,0,1,1,1,2,3,3,3,3,3,3,3,3,3]


>>>



IOW理解是附加而不是扩展。除了

之外你移动了你从

里面附加的值('s')到了理解的开头然后删除

换行符和冒号[在一些行李箱中]。


Rob。

-
http://www.victim-prime.dsl.pipex.com/


Please help my poor brain :) Every time I try to do a list
comprehension I find I just don''t comprehend ...

Anyway, I have the following bit of code:

seq = [2, 3, 1, 9]
tmp = []
for a in range(len(seq)):
tmp.extend([a]*seq[a])

which correctly returns:

[0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3]

Question is, can I do this as a list comprehension?

Thanks!

解决方案

"bv**@xplornet.com" <bv**@xplornet.comwrites:

seq = [2, 3, 1, 9]
tmp = []
for a in range(len(seq)):
tmp.extend([a]*seq[a])

which correctly returns:

[0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3]

Question is, can I do this as a list comprehension?

import operator
x = reduce(operator.add, ([i]*a for i,a in enumerate(seq)), [])


Paul Rubin <http://ph****@NOSPAM.invalidwrites:

Question is, can I do this as a list comprehension?


import operator
x = reduce(operator.add, ([i]*a for i,a in enumerate(seq)), [])

Maybe more in the iterative spirit:

import itertools
seq = [2, 3, 1, 9]
x = itertools.chain(*([i]*a for i,a in enumerate(seq)))


bv**@xplornet.com wrote in news:1157758817.446690.105620
@i42g2000cwa.googlegroups.com in comp.lang.python:

Please help my poor brain :) Every time I try to do a list
comprehension I find I just don''t comprehend ...

Anyway, I have the following bit of code:

seq = [2, 3, 1, 9]
tmp = []
for a in range(len(seq)):
tmp.extend([a]*seq[a])

which correctly returns:

[0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3]

Question is, can I do this as a list comprehension?

Well apparently I can, though it did take me 2 goes:

>>seq = [2, 3, 1, 9]
sum( [ [a]*seq[a] for a in range(len(seq)) ] )

Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
sum( [ [a]*seq[a] for a in range(len(seq)) ] )
TypeError: unsupported operand type(s) for +: ''int'' and ''list''

>>sum( [ [a]*seq[a] for a in range(len(seq)) ], [] )

[0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3]

>>>

But thats just me showing off what a filthy programmer I am.

My third attempt went like:

>>[ x for a in a in range(len(seq)) for x in [a] * seq[a] ]

Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
[ x for a in a in range(len(seq)) for x in [a] * seq[a] ]
TypeError: iteration over non-sequence

Ah the perils of cut-n-paste (and my what a strange error),
But my forth attemp yeilded (If that''s a pun I do appologise)
this:

>>[ x for a in range(len(seq)) for x in [a] * seq[a] ]

[0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3]

>>>

Which is possibly something you might of expected.

Note the unrolled version goes:

>>tmp = []
for a in range(len(seq)):

for x in [a] * seq[a]:
tmp.append( x )

>>tmp

[0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3]

>>>

IOW a comprehension appends rather than extends. Other than
that you move the value you''re appending from the inside of
the loop(''s) to the begining of the comprehension then remove
newlines and colon''s [inside some brakets ofcourse].

Rob.
--
http://www.victim-prime.dsl.pipex.com/


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

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