Python 列表中的列表
除了获得理解之外,我没有这样做的真正原因,但我正在尝试使用列表理解创建列表列表的列表。
我可以很好地创建列表列表:
In[1]: [j for j in [range(3,k) for k in [k for k in range(5,10)]]]
Out[1]: [[3, 4], [3, 4, 5], [3, 4, 5, 6], [3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8]]
并且我可以从结果中创建列表列表列表,例如:
In [2]: [range(0,i) for i in [3,4]]
Out[2]: [[0, 1, 2], [0, 1, 2, 3]]
In [3]: [range(0,i) for i in j]
Out[3]:
[[0, 1, 2],
[0, 1, 2, 3],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5, 6],
[0, 1, 2, 3, 4, 5, 6, 7]]
但是当我尝试将其组合成单个语句时,它会出错:
In [4]: [range(0,i) for i in [j for j in [range(3,k) for k in [k for k in range(5,10)]]]]
---------------------------------------------------------------------------
TypeError: range() integer end argument expected, got list.
我是否缺少一些括号某处?
I don't have a real reason for doing this, other than to gain understanding, but I'm trying to create a list of lists of lists using list comprehension.
I can create a list of lists just fine:
In[1]: [j for j in [range(3,k) for k in [k for k in range(5,10)]]]
Out[1]: [[3, 4], [3, 4, 5], [3, 4, 5, 6], [3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8]]
And I can create a list of lists of lists from either the results of that, for example:
In [2]: [range(0,i) for i in [3,4]]
Out[2]: [[0, 1, 2], [0, 1, 2, 3]]
In [3]: [range(0,i) for i in j]
Out[3]:
[[0, 1, 2],
[0, 1, 2, 3],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5, 6],
[0, 1, 2, 3, 4, 5, 6, 7]]
But when I try to combine it into a single statement it goes awry:
In [4]: [range(0,i) for i in [j for j in [range(3,k) for k in [k for k in range(5,10)]]]]
---------------------------------------------------------------------------
TypeError: range() integer end argument expected, got list.
Am I missing some brackets somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试以下操作:
这会产生以下列表列表:
了解列表理解中发生的情况的最佳方法是尝试将其展开到正常的 for 循环中,让我们尝试一下您的循环,然后我的看看会发生什么区别在于:
在这个
x
的末尾将相当于你的列表理解,但是当然这个代码不会工作,因为b
(和c) 将是列表的列表,所以
i
将是一个列表,而range(0, i)
将导致错误。现在显然这不是您想要做的,因为您真正希望看到的是那些嵌套的for
循环,而不是一个接着一个。让我们看看我的工作原理:
希望这有助于澄清!
Try the following:
This results in the following list of lists of lists:
The best way to understand what is happening in a list comprehension is to try to roll it out into normal for loops, lets try that with yours and then mine to see what the difference is:
At the end of this
x
would be equivalent to your list comprehension, however of course this code will not work becauseb
(andc
) will be lists of lists, soi
will be a list andrange(0, i)
will cause an error. Now obviously this is not what you intended to do, since what you would really like to see is thosefor
loops nested instead of one after the other.Lets look at how mine works:
Hope this helped to clarify!
从你的问题来看:
range
采用整数参数所以当你
这样做时相当于说
当然,这会失败,因为每个
i
都是一个list
和range
不接受list
参数。这里的其他答案向您展示了如何修复错误。此回复是为了解释您最初的方法出了什么问题
希望这会有所帮助
From your question:
range
takes integer parametersSo when you do
it is the equivalent of saying
Of course, this will fail because each
i
is alist
andrange
doesn't takelist
parameters.Other answers here show you how to fix your error. This response was to explain what went wrong with your initial approach
Hope this helps
为了解决这个问题,我一次运行列表理解的每一步。
你的问题就在这里,因为它将列表提供给下一个
range()
而不是整数:你可以使用 lambda 和 reduce 压平这个列表:
但是你仍然只会嵌套两个列表深:
如果你想深入三个列表,您需要重新考虑您的程序流程。列表推导式最适合处理迭代器中最外层的对象。如果您在
for
语句的左侧和右侧都使用了列表推导式,则可以嵌套得更深:To troubleshoot this, I ran each step of the list comprehension at a time.
Your problem is here, because it feeds lists to the next
range()
instead of ints:You can flatten this list using lambda and reduce:
But you will still only nest two lists deep:
If you want to go three lists deep, you need to reconsider your program flow. List comprehensions are best suited for working with the outermost objects in an iterator. If you used list comprehensions on the left side of the
for
statement as well as the right, you could nest more deeply:那是因为:
您可能想做:
您当然知道您的第一个语句可以简化为:
??
That's because:
You probably wanted to do:
You do of course know that your first statement can be reduced to :
??
因为 i 是一个列表,所以您也需要对其进行列表理解:
在您的代码中:
此外,您还有一个不必要的理解。这将产生相同的结果:
顺便说一句,如果只是为了代码的可读性,列表推导式就变得比递归函数或简单的嵌套 for 循环不太理想。
Because i is a list, you need list comprehend on that too:
In your code:
Also, you have one unnecessary comprehension in there. This will produce the same result:
As an aside, this is about the point where a list comprehension becomes less desirable than a recursive function or simple nested for loops, if only for the sake of code readability.
显然,在第一个范围 (range(0,i)) 中,i 是
[[3, 4], [3, 4, 5], [ 3, 4, 5, 6], [3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8]]
在调用之前,您可能需要展平该列表范围函数与 它。
Apparently, in the first range (range(0,i)), i is
[[3, 4], [3, 4, 5], [3, 4, 5, 6], [3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8]]
You probably need to flatten that list before you call the range function with it.