理解嵌套列表理解 [英] Understanding nested list comprehension

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

问题描述

我想了解嵌套列表理解.下面,我列出了一个列表推导式表达式及其 for 循环等效项.
我想知道我对这些的理解是否正确.

例如

[(min([row[i] for row in rows]),max([row[i] for row in rows]))对于范围内的 i(len(rows[0]))]

相当于

result=[]对于范围内的 i(len(rows[0])):内部结果=[]对于行中的行:innerResult.append(row[i])内部结果2=[]对于行中的行:innerResult2.append(row[i])元组=(min(innerResult),max(innerResult2))结果.附加(元组)

如果我可以概括,我猜

[exp2([exp1 for x in xSet]) for y in ySet]

form 可以翻译成以下形式.(我希望我是对的)

result=[]对于 ySet 中的 y:内部结果 =[]对于 xSet 中的 x:innerResult.append(exp1)exp2Result = exp2(innerResult)结果.追加(exp2Result)

对于更简单的情况,

[exp1 for x in xSet for y in ySet]

等于

result=[]对于 xSet 中的 x:对于 ySet 中的 y:结果.附加(exp1)

然而,

[[exp1 for x in xSet] for y in ySet]

等于

result=[]对于 ySet 中的 y:内部结果=[]对于 xSet 中的 x:innerResult.append(exp1)result.append(innerResult)

我在等效于复杂列表的循环表达式上问了一个类似的问题理解
在了解其内部作用后,那里给出的答案重建了该形式.
我想知道它是如何系统地工作的,所以我可以将这个概念应用到其他稍微不同的例子中.

解决方案

简短的回答是:是的,你的理解是正确的.

只有一个问题:您通常在 Python 代码中使用嵌套列表推导的方式是对多维数组进行操作.

一个典型的例子是当你对矩阵进行操作时:

<预><代码>>>>矩阵 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]>>>[[el - 1 表示行中的 el] 表示矩阵中的行][[0, 1, 2], [3, 4, 5], [6, 7, 8]]

如您所见,嵌套"通过对矩阵的每个维度进行操作来实现.

在您提供的示例中,似乎 ySet [不幸的名字顺便说一句,如 sets 是 python 提供的类型之一] 只是一个通用计数器,这使得理解下面发生的事情有点困难引擎盖.

至于你的第一个例子:

<预><代码>>>>行 = ([1, 2, 3], [10, 20, 30])>>>[(min([row[i] for row in rows]),max([row[i] for row in rows])) for i in range(len(rows[0]))][(1, 10), (2, 20), (3, 30)]

您可能希望查看 zip 内置函数:

<预><代码>>>>zip(行[0],行[1])[(1, 10), (2, 20), (3, 30)]

或最大限度地简洁和优雅:

<预><代码>>>>zip(*行)[(1, 10), (2, 20), (3, 30)]

HTH!

I want to understand nested list comprehension. Below, I listed a list comprehension expression and their for loop equivalent.
I wonder if my understanding is correct on those.

For example,

[(min([row[i] for row in rows]),max([row[i] for row in rows])) 
for i in range(len(rows[0]))]

is equivalent to

result=[]
for i in range(len(rows[0])):
  innerResult=[]
  for row in rows:
    innerResult.append(row[i])
  innerResult2=[]
  for row in rows:
    innerResult2.append(row[i])
  tuple=(min(innerResult), max(innerResult2))
  result.append(tuple)

If I may generalize, I guess

[exp2([exp1 for x in xSet]) for y in ySet]

form can be translated to the following. (I hope I'm correct on this)

result=[]
for y in ySet:
  innerResult =[]
  for x in xSet:
    innerResult.append(exp1)
  exp2Result = exp2(innerResult)
  result.append(exp2Result)

For simpler case,

[exp1 for x in xSet for y in ySet] 

is equal to

result=[] 
for x in xSet:
  for y in ySet: 
    result.append(exp1)

whereas,

[[exp1 for x in xSet] for y in ySet]

is equal to

result=[]
for y in ySet:
  innerResult=[]
  for x in xSet:
    innerResult.append(exp1)
  result.append(innerResult)

I asked a similar question on Equivalent for loop expression for complex list comprehension
The answers given there reconstruct the form after understanding what it does internally.
I'd like to know how it works systematically so I can apply the concept to other slightly varying examples.

解决方案

The short answer is: yes, you are correct in your understanding.

There's only a catch: the way you normally use nested list comprehension in python code is to operate on multidimensional arrays.

A typical example is when you operate on matrices:

>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [[el - 1 for el in row] for row in matrix]
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]

As you can see the "nesting" works by operating on each dimension of the matrix.

In the examples you provided, it seems that ySet [unfortunate name btw, as sets are one of the types provided with python] is just a generic counter, which makes a bit harder to follow what is going on under the hood.

As for your first example:

>>> rows = ([1, 2, 3], [10, 20, 30])
>>> [(min([row[i] for row in rows]),max([row[i] for row in rows])) for i in range(len(rows[0]))]
[(1, 10), (2, 20), (3, 30)]

You might wish to look into the zip built-in function:

>>> zip(rows[0], rows[1])
[(1, 10), (2, 20), (3, 30)]

or for maximum brevity and elegance:

>>> zip(*rows)
[(1, 10), (2, 20), (3, 30)]

HTH!

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

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