简体   繁体   English

字典,列表或集合末尾的额外逗号是否有任何特殊含义?

[英]does the extra comma at the end of a dict, list or set has any special meaning?

I noticed by chance that adding an extra separator comma at the end of a list, dict or set is syntactically correct and does not seem to add anything to the data structure: 我偶然注意到在列表,dict或set的末尾添加一个额外的分隔符逗号在语法上是正确的,并且似乎没有向数据结构添加任何内容:

In [1]: d1 = {'a': 1, 'b': 2}

In [2]: d2 = {'c': 10, 'd': 20,}

In [3]: d1
Out[3]: {'a': 1, 'b': 2}

In [4]: d2
Out[4]: {'c': 10, 'd': 20}

Does it have any special meaning or usage? 它有什么特殊含义或用法吗? The only one I found is to explicit a data structure during an initialization: 我发现的唯一一个是在初始化期间显式化数据结构:

In [14]: r = (1)

In [15]: r
Out[15]: 1

In [16]: r = (1,)

In [17]: r
Out[17]: (1,)

It has no special meaning in a list or dictionary, but can be useful when using source code change management tools, see below. 它在列表或字典中没有特殊含义,但在使用源代码更改管理工具时非常有用,请参见下文。

Non-empty tuples are defined by using a comma between elements, the parentheses are optional and only required in contexts where the comma could have a different meaning. 通过在元素之间使用逗号来定义非空元组,括号是可选的,并且仅在逗号可具有不同含义的上下文中需要。

Because the comma defines the tuple, you need at least one comma if there is just the one element: 因为逗号定义了元组,所以如果只有一个元素,则至少需要一个逗号:

>>> 1
1
>>> 1,
(1,)
>>> type((1,)) # need parens to distinguish comma from the argument separator
<type 'tuple'>

The empty tuple is defined by using empty parentheses: 使用空括号定义空元组:

>>> type(())
<type 'tuple'>

The trailing comma can be helpful in minimising how many lines changed when adding new lines; 尾随逗号有助于最小化添加新行时更改的行数; adding an additional line to a dictionary with a trailing comma would not change the last existing entry: 使用尾随逗号向字典中添加其他行不会更改上一个现有条目:

a_value = {
    key1: value1,
    key2: value2,
    # inserting here doesn't require adding a comma
    # to the preceding line.
}

It's useful for consistency across multiple lines: 它对多行的一致性很有用:

d1 = {
    'a': 1,
    'b': 2,
}

You can rearrange or add lines without changing commas. 您可以重新排列或添加行而不更改逗号。 It doesn't change anything about the information represented. 它不会改变所代表的信息。

You can use dis module to verify that behaviour of both objects is exactly the same. 您可以使用dis模块验证两个对象的行为是否完全相同。

In [10]: import dis

In [11]: def make_dict():
    return {"a": 1, "b": 2}
   ....: 

In [12]: def make_dict_2():
    return {"a": 1, "b": 2,}
   ....: 

In [13]: dis.dis(make_dict)
  2           0 BUILD_MAP                2
              3 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 ('a')
              9 STORE_MAP           
             10 LOAD_CONST               3 (2)
             13 LOAD_CONST               4 ('b')
             16 STORE_MAP           
             17 RETURN_VALUE        

In [14]: dis.dis(make_dict_2)
  2           0 BUILD_MAP                2
              3 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 ('a')
              9 STORE_MAP           
             10 LOAD_CONST               3 (2)
             13 LOAD_CONST               4 ('b')
             16 STORE_MAP           
             17 RETURN_VALUE 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “~”字符在 python 3 中有什么特殊含义吗? - Does the “~” character have any special meaning in python 3? len(list) 末尾的逗号是什么意思? - What does the comma at end of len(list) mean? 除了 dict、list、set 和 tuple 之外,还有没有内置的容器? - Are there any built-in containers other than dict, list, set, and tuple? (list | set | dict)包含yield表达式的理解不返回(list | set | dict) - (list|set|dict) comprehension containing a yield expression does not return a (list|set|dict) AttributeError: &#39;dict&#39; 对象没有属性 &#39;split&#39; 将逗号分隔的标签转换为列表 - AttributeError: 'dict' object has no attribute 'split' to convert comma separated labels into a list 将嵌套的dict转换为仅包含具有唯一键集的列表项的dict - Convert a nested dict to a dict that only include list items that has a unique set of keys 为什么 Python 的 dict.keys() 返回一个列表而不是一个集合? - Why does Python's dict.keys() return a list and not a set? 从文件列表中加载逗号分隔的字典 - loading comma separated dict from a list in a file &#39;p&#39;在Django中有特殊含义吗? - Does 'p' have a special meaning in Django? Python 检查 dict 列表是否按正确顺序排列或有额外的 dict - Python check a list of dict if it is in correct order or got extra dict
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM