在Python中,对列表的理解与对元组的区别是什么? [英] In Python, whats the difference between a list comprehension with a list and a tuple?

查看:43
本文介绍了在Python中,对列表的理解与对元组的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在玩iPython时,我惊讶地发现给定对象列表 f ,每个对象都支持某种方法 x()(例如,打印出>嗨!" ),表达式:

Playing around with iPython, I was surprised to discover that given a list f of objects each supporting some method x() (that, say, prints out "Hi!"), the expression:

(y.x() for y in f)

在语义上不等同于

[y.x() for y in f]

第一个(以元组为输出)会生成一个生成器表达式,除非我对其进行迭代,否则该表达式不会被求值,而带有列表的那个实际上会使生成立即发生:

The first one (with the tuple as output) results in a generator expression that is not evaluated unless I iterate over it, whereas the one with the list actually causes the generation to happen immediately:

In [30]: (y.x() for y in f)
Out[30]: <generator object <genexpr> at 0x2d78d70>

但是

In [31]: [y.x() for y in f]
Hi!
Hi!
Hi!
Hi!

这似乎是违反直觉的.

问题:为什么第一个表达式不生成从生成器获取的值的元组,而不是生成列表的方式?

Question: Why is the first expression not generating a tuple of the values obtained from the generator the way the list is being built?

更新:随着我进一步注视着这一点,我意识到也许在第一种情况下发生的事情是Python只是在构建一个包含生成器的元组,而不是像在生成器中那样评估生成器.第二种情况.

Update: As I stare at this more, I realize that perhaps what's happening in the first case is that Python is just building a tuple containing a generator rather than evaluating the generator as it is in the second case.

是不是真的不可能直接 获得元组作为生成列表理解的结果?(我知道我可以做 tuple([y.x()for y in f])).我没有用例,这纯粹是我的理解.

So is it true that it is not possible to directly get a tuple as the result of generating a list comprehension? (I understand I can do tuple([y.x() for y in f])). I don't have a use case, this is purely for my understanding.

推荐答案

某些人将元组视为只读列表,并且在某些情况下有效.但这不是元组的语义意图.列表旨在用于同类元素(具有共享类型的元素)的可变长度结构.元组用于固定长度的结构,其中每个分度位置都包含某种类型的元素.

Some people treat tuples as read-only lists, and that works in some contexts. But that is not the semantic intention of tuples. Lists are intended to be used for variable length structures of homogeneous elements (elements with a shared type). Tuples are intended for fixed length structures in which each indexed position contains a certain type of element.

enumerate(lst)是一个示例.它返回一个可变长度的元组列表.每个元组都有两个元素,第一个元素始终是整数,第二个元素来自 lst .

enumerate(lst) is an example of this. It returns a variable-length list of tuples. Each tuple has exactly two elements, the first of which is always an integer, and the second of which is from lst.

基于这种理解,元组生成器有点荒谬.这可能就是原因.

With that understanding, a tuple generator is a bit nonsensical. That is probably why.

对于直接生成元组,您可以比示例做得更好.这也可以:

As for directly generating a tuple, you can do a little bit better than your example. This also works:

tuple(y.x() for y in f)

将生成器传递给 tuple()方法,该方法构造一个元组,但不创建中间的 list .

That passes a generator to the tuple() method, which constructs a tuple, but doesn't create an intermediate list.

要清楚,(f中y的yx())没有涉及 tuple ,但是 t = 1时有一个tuple,2、3 .组成元组的不是括号.这是逗号.

To be clear, there is no tuple involved in (y.x() for y in f), but there is a tuple in t = 1, 2, 3. It's not the parens that make the tuple. It's the commas.

这篇关于在Python中,对列表的理解与对元组的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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