在Python中,你可以在一个列表推导式中有多个迭代器,比如

[(x,y) for x in a for y in b]

我知道Python的列表推导式的嵌套循环语义。

我的问题是:理解中的一个迭代器可以指向另一个迭代器吗?换句话说:我能得到这样的东西吗?

[x for x in a for a in b]

外部循环的当前值是内部循环的迭代器?

举个例子,如果我有一个嵌套列表:

a=[[1,2],[3,4]]

要实现这个结果,列表理解表达式是什么:

[1,2,3,4]

?? (请只列出理解性的答案,因为这是我想知道的)。


当前回答

这个记忆技巧对我帮助很大:

[< currend_value > < outer_loop1 > < inner_loop2 > < inner_loop3 >…]< OPTIONAL_IF >)

现在你可以考虑返回+外循环 作为唯一的正义秩序

知道了上面,列表综合的顺序即使是3个循环看起来也很简单:


c=[111, 222, 333]
b=[11, 22, 33]
a=[1, 2, 3]

print(
  [
    (i, j, k)                            # <RETURNED_VALUE> 
    for i in a for j in b for k in c     # in order: loop1, loop2, loop3
    if i < 2 and j < 20 and k < 200      # <OPTIONAL_IF>
  ]
)
[(1, 11, 111)]

因为上面只是一个:

for i in a:                         # outer loop1 GOES SECOND
  for j in b:                       # inner loop2 GOES THIRD
    for k in c:                     # inner loop3 GOES FOURTH
      if i < 2 and j < 20 and k < 200:
        print((i, j, k))            # returned value GOES FIRST

对于迭代一个嵌套的列表/结构,技术是相同的: 对于a这个问题:

a = [[1,2],[3,4]]
[i2    for i1 in a      for i2 in i1]
which return [1, 2, 3, 4]

对于另一个嵌套级别

a = [[[1, 2], [3, 4]], [[5, 6], [7, 8, 9]], [[10]]]
[i3    for i1 in a      for i2 in i1     for i3 in i2]
which return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

等等

其他回答

ThomasH已经补充了一个很好的答案,但我想展示发生了什么:

>>> a = [[1, 2], [3, 4]]
>>> [x for x in b for b in a]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined

>>> [x for b in a for x in b]
[1, 2, 3, 4]
>>> [x for x in b for b in a]
[3, 3, 4, 4]

我猜Python是从左到右解析列表理解的。这意味着,发生的第一个for循环将首先执行。

第二个“问题”是b被“泄露”出了列表理解。在第一个成功的列表理解b ==[3,4]。

假设你有一个充满句子的文本,你想要一个单词数组。

# Without list comprehension
list_of_words = []
for sentence in text:
    for word in sentence:
       list_of_words.append(word)
return list_of_words

我喜欢将列表理解理解为横向扩展代码。

试着把它分解成:

# List Comprehension 
[word for sentence in text for word in sentence]

例子:

>>> text = (("Hi", "Steve!"), ("What's", "up?"))
>>> [word for sentence in text for word in sentence]
['Hi', 'Steve!', "What's", 'up?']

这也适用于生成器

>>> text = (("Hi", "Steve!"), ("What's", "up?"))
>>> gen = (word for sentence in text for word in sentence)
>>> for word in gen: print(word)
Hi
Steve!
What's
up?

如果你想保持多维数组,就应该嵌套数组括号。参见下面的示例,其中每个元素都添加了一个。

>>> a = [[1, 2], [3, 4]]

>>> [[col +1 for col in row] for row in a]
[[2, 3], [4, 5]]

>>> [col +1 for row in a for col in row]
[2, 3, 4, 5]

这个flat_nlevel函数递归调用嵌套的list1来转换到一个级别。试试这个

def flatten_nlevel(list1, flat_list):
    for sublist in list1:
        if isinstance(sublist, type(list)):        
            flatten_nlevel(sublist, flat_list)
        else:
            flat_list.append(sublist)

list1 = [1,[1,[2,3,[4,6]],4],5]

items = []
flatten_nlevel(list1,items)
print(items)

输出:

[1, 1, 2, 3, 4, 6, 4, 5]

哎呀,我想我找到了答案:我没有足够关心哪个循环是内部的,哪个是外部的。列表推导式应该是这样的:

[x for b in a for x in b]

为了得到想要的结果,一个当前值可以作为下一个循环的迭代器。