你掌握了列表理解吗? [英] do you master list comprehensions?
问题描述
这是关于列表推导[lc]的问题。
的问题很愚蠢,因为我可以不用[lc];但是我提出这个问题是因为我很好奇。
这个:
Here is a question about list comprehensions [lc]. The
question is dumb because I can do without [lc]; but I am
posing the question because I am curious.
This:
data = [[''foo'',''bar'',''baz''],[''my'',''your''],[' 'holy'',''grail'']]
结果= []
用于数据中的d:
....用于w中的d:
.... result.append(w)打印结果
data = [[''foo'',''bar'',''baz''],[''my'',''your''],[''holy'',''grail'']]
result = []
for d in data: .... for w in d:
.... result.append(w) print result
[''foo'',''bar'',''baz'' ,''我的',''你'',''圣洁'',''圣杯'']
将所有单词放入列表中,就像我想要的那样。 br />
怎么用[lc]而不是for-loops做这个呢?
我试过[[w for L in L] for funnies L in data],
这是正确的语法,但你永远不会猜到。
我知道,傻!不需要[lc]!所以这是我的
问题。我相信使用[lc]的单线将非常有用。就像研究LISP一样。
-
我希望电视上有一个旋钮可以调高智能。
有一个旋钮称为'亮度'',但它不起作用。
- 加拉格尔
[''foo'', ''bar'', ''baz'', ''my'', ''your'', ''holy'', ''grail'']
puts all the words in a list, like I want.
How to do this with [lc] instead of for-loops?
I tried funnies like [[w for w in L] for L in data],
that is correct syntax, but you''d never guess.
I know, silly! No need for [lc]! So there''s my
question. I am sure a one-liner using [lc] will be very
enlightening. Like studying LISP.
--
I wish there was a knob on the TV to turn up the intelligence.
There''s a knob called `brightness'', but it doesn''t work.
-- Gallagher
推荐答案
Will Stuyvesant写道:
Will Stuyvesant wrote:
我尝试过像[[w for L in L]中的L一样的数据中的L],
这是正确的语法,但你永远猜不到。
I tried funnies like [[w for w in L] for L in data],
that is correct syntax, but you''d never guess.
这绝对是正确的。它根本不是一个小丑。如果你发现它很奇怪
它只是因为你不习惯列出理解者。
在这种情况下你可能会更舒服:
data = [[''foo'',''bar'',''baz''],[''my'',''your''],[' 'holy'',''grail'']]
result = []
for l in data:
result + = l < br $>
-
hilsen /问候丹麦Max M
http://www.mxm.dk/
IT''s Mad Science
That is absolutely correct. It''s not a funnie at all. If you find it odd
it''s only because you are not used to list comprehensiones.
In that case you might be more comfortable with:
data = [[''foo'',''bar'',''baz''],[''my'',''your''],[''holy'',''grail'']]
result = []
for l in data:
result += l
--
hilsen/regards Max M, Denmark
http://www.mxm.dk/
IT''s Mad Science
请问Stuyvesant写道:
Will Stuyvesant wrote:
data = [[''foo'',''bar'', ''baz''],[''my'',''your''],[''holy'',''grail'']]
结果= []
数据:
...代表w:
... result.append(w)
打印结果
[''foo'',' bar'',''baz'',''my'',''your'',''holy'',''grail'']
data = [[''foo'',''bar'',''baz''],[''my'',''your''],[''holy'',''grail'']]
result = []
for d in data:
... for w in d:
... result.append(w)
print result
[''foo'', ''bar'', ''baz'', ''my'', ''your'', ''holy'', ''grail'']
利用你可以在
列表理解中拥有多个''for'这一事实:
Take advantage of the fact that you can have more than one ''for'' in a
list comprehension:
data = [[''foo' ',''bar'',''baz''],[''my'',''your''],[''holy'',''grail'']]
[item for item_list中项目数据的item_list]
data = [[''foo'',''bar'',''baz''],[''my'',''your''],[''holy'',''grail'']]
[item for item_list in data for item in item_list]
[''foo'',''bar'',''baz'',''我的'',''你'',''圣洁'',''圣杯'']
史蒂夫
[''foo'', ''bar'', ''baz'', ''my'', ''your'', ''holy'', ''grail'']
Steve
Will Stuyvesant写道:
Will Stuyvesant wrote:
data = [[''foo'',''bar'',''baz ''],[''我'',''你''],[''圣洁'',''圣杯']]
结果= []
用于数据中的d :. ..对于w来说:
... result.append(w)打印结果[''foo'',''bar'',''baz'',''my'',''your'' ,''holy'',''grail'']
将所有单词放在列表中,就像我想要的那样。
如何用[lc]代替for-loops?
data = [[''foo'',''bar'',''baz''],[''my'',''your''],[''holy'',''grail'']]
result = []
for d in data: ... for w in d:
... result.append(w) print result [''foo'', ''bar'', ''baz'', ''my'', ''your'', ''holy'', ''grail'']
puts all the words in a list, like I want.
How to do this with [lc] instead of for-loops?
data = [[''foo'',''bar'',''baz''],[''my''' ,''你的'',[''圣洁'',''圣杯']]
[w为d中的w数据]
data = [[''foo'',''bar'',''baz''],[''my'',''your''],[''holy'',''grail'']]
[w for d in data for w in d]
[''foo'',''bar'',''baz'',''my'',''your'',''holy'',''grail'']
看看列表推导中的for表达式与嵌套for循环的
完全匹配?这就是它的全部。
彼得
[''foo'', ''bar'', ''baz'', ''my'', ''your'', ''holy'', ''grail'']
See how the for expressions in the list comprehension exactly match your
nested for loops? That''s all there is to it.
Peter
这篇关于你掌握了列表理解吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!