这些 Python 符号是什么:`[ [] ] * n` 和 `(i,)` [英] What are these Python notations: `[ [] ] * n` and `(i,)`

查看:73
本文介绍了这些 Python 符号是什么:`[ [] ] * n` 和 `(i,)`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以澄清 Python 中的这 2 个符号吗:

Can someone please clarify these 2 notations in Python:

  • [ [] ] * n: apparently this creates n references to the same object (in this case, an empty list). In which situations is this useful?
  • (i,): I have seen some people use this "trailing comma" notation (example: Generating all size k subsets of {0, 1, 2, ... n-1} in the definition of the sets function). What does it mean?

推荐答案

[ [] ] * n 创建对同一个空列表对象的 n 引用.这几乎总是一个错误,因为通常您最终会对 n 列表之一使用破坏性操作,然后当它应用于外部列表的所有"元素时感到惊讶.

[ [] ] * n creates n references to the same empty list object. This is almost always a bug, since usually you'll end up using a destructive operation on one of the n lists and then being surprised when it applies to "all" of the elements of your outer list.

然而,[0] * n 创建 n 对同一个整数零对象的引用.像这样的表达式可以方便地初始化计数器列表(您可能会继续替换列表中的各个插槽).由于整数是不可变的,因此不存在意外更改所有"零的危险.任何其他一直不可变"类型(不一定是元组,因为虽然元组本身是不可变的,但它可以包含可变值)也是如此.

However [0] * n creates n references to the same integer-zero object. Expressions like this can be handy for initializing a list of counters (you would presumably then go on to replace individual slots in the list). Since integers are immutable, there is no danger of altering "all" of the zeroes accidentally. The same goes for any other "immutable all the way down" type (not necessarily tuples, since although the tuple itself is immutable it can contain mutable values).

所以这个列表乘法"操作不是无用的.但我建议避免使用它除非复制保证的不可变值列表的特定情况.虽然可以安全地使用可变值列表的乘法,但它非常脆弱,而且经常是错误的根本原因,即使这是你的意思,你最好在一个稍微详细一点的方式,使您很明显是认真的.

So this "list multiplication" operation isn't useless. But I would advise avoiding it except for the specific case of replicating a list of guaranteed immutable values. While multiplying a list of mutable values can be used safely, it's so fragile and so often the root cause of a bug that even when that is what you mean to do you're better of doing it in a slightly more verbose way that makes it obvious you really mean it.

至于 (i,),Python 允许在列表、字典、元组和集合文本中使用额外"逗号.例如,以下都是有效的:

As for (i,), Python allows an "extra" comma in list, dictionary, tuple, and set literals. For example, the following are all valid:

(1, 2, 3,)
[1, 2, 3,]
{1: 'one', 2: 'two', 3: 'three',}
{1, 2, 3,}

这与省略尾随逗号没有什么不同,它只是允许作为可选的附加项.

This doesn't do anything different than if you'd left out the trailing comma, it's simply allowed as optional extra.

回到(i,).这只是一个带有尾随逗号的项目的元组.但有一个问题;对于只有一项的元组的特殊情况,尾随逗号不是可选的附加项,而是强制.那是因为一个单元组"会被写成 (i),但是这种句法形式已经被用于使用括号对子表达式进行分组(例如,你无法以其他方式判断 (i)>(1 + 1) * 2 应该产生 4(2, 2)).

Back to (i,). This is simply a tuple of one item with the trailing comma. But there's a catch; for the special case of tuples with only one item the trailing comma isn't an optional extra, it's mandatory. That's because a "one-tuple" would otherwise be written (i), but that syntactic form is already taken for using parentheses to group a sub-expression (e.g. you couldn't otherwise tell whether (1 + 1) * 2 was supposed to produce 4 or (2, 2)).

顺便说一句,当您将长元组/列表/字典/集拆分为多行时,有时您可能想要使用可选的尾随逗号的原因更明显:

As an aside, the reason why you might want to use the optional trailing comma sometimes is more obvious when you're splitting a long tuple/list/dictionary/set over multiple lines:

foo = [
   'this',
   'is',
   'a',
   'long',
   'list',
]

这种风格的写作让我可以简单地通过重新排列源代码行来重新排列列表;如果我重新订购的项目之一是列表中的最后一个,我就不必去清理逗号.

Writing in this style allows me to re-order the list simply by re-ordering the source code lines; I don't have to then go and clean up the commas if one of the items I re-ordered was the last one in the list.

同样,如果我使用版本控制,在最后一个之后添加更多项目不会改变与先前最终项目的行,因此差异是纯粹的添加.如果最后一行没有逗号,您需要添加一个以添加更多项目,这会使更改记录为对该行的编辑,这使得合并冲突(稍微)更有可能并且历史日志(稍微)更难阅读.

Similarly if I'm using version control, adding more items after the last one doesn't change the line with the previously-final item, so the diff is a pure addition. If the last line didn't have a comma you would need to add one to add more items, which makes the change recorded as an edit to that line, which makes merge conflicts (slightly) more likely and history logs (slightly) harder to read.

这篇关于这些 Python 符号是什么:`[ [] ] * n` 和 `(i,)`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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