简体   繁体   English

Python:为什么字典类型数据可以自动排除某些项目?

[英]Python: why a dictionary type data could automatically exclude some items?

I created a dictionary containing six items as below: 我创建了一个包含以下六个项目的字典:

 >>> dict1 = {
'A': ['A','A'],
'AB':['A','B'], 
'A':['A','O'],
'B':['B','B'],
'B':['B','O'],
'O':['O','O']
}

But when I check the dictionary I found that the items "{'A': ['A', 'A'], 'B': ['B', 'B']}" have been excluded. 但是当我检查字典时,我发现项目"{'A': ['A', 'A'], 'B': ['B', 'B']}"已被排除。

>>> dict1
Out[19]: {'A': ['A', 'O'], 'AB': ['A', 'B'], 'B': ['B', 'O'], 'O': ['O', 'O']}
>>> len(dict1)
Out[17]: 4

However if I create a new dictionary with the excluded items.It becomes normal. 但是,如果我使用排除的项目创建一个新的字典。它变得正常。

>>> dict2 ={'A': ['A', 'A'], 'B': ['B', 'B']}
>>> dict2
Out[21]: {'A': ['A', 'A'], 'B': ['B', 'B']}

Could anybody explain me why is that? 谁能解释我为什么会这样?

You cannot have duplicate keys but you can have multiple values. 您不能拥有重复的密钥,但可以拥有多个值。 In other words, each key is unique. 换句话说, 每个键都是唯一的。

So each time you assign new values to the same key, you override the previous values of the key. 因此,每次为同一个键分配新值时,都会覆盖键的先前值。

A way to assign 2 values (or lists) like in your example can be the following: 如示例所示,分配2个值 (或列表)的方法如下:

dict1 = {'A': [['A','A'],['A','O']], 'B':[['B','B'],['B','O']], 'O':['O','O'], 'AB':['A','B']}

Result 结果

{'A': [['A', 'A'], ['A', 'O']], 'B': [['B', 'B'], ['B', 'O']], 'AB': ['A', 'B'], 'O': ['O', 'O']}

Finally, you can access each key as follows: 最后,您可以按如下方式访问每个密钥:

dict1['A']

Result 结果

[['A', 'A'], ['A', 'O']]

This seems to be what you want to do. 这似乎是你想要做的。

Hope this helps. 希望这可以帮助。

The thing with dictionaries in Python is that each key is unique . Python中的词典是每个键都是唯一的 That is, when you add an existing entry the previous value stored is overwritten by the new one. 也就是说,当您添加现有条目时,存储的先前值将被新条目覆盖。

When you typed: 当你输入:

dict1 = {
  'A': ['A','A'],
  'AB':['A','B'], 
  'A':['A','O'], # Overrides ['A', 'A']
  'B':['B','B'],
  'B':['B','O'], # Overrides previous entry
  'O':['O','O']
}

You gave the dictionary two values for the keys 'A' and 'B' . 你给了字典两个键'A''B' That is you asked the dict to change the value previously stored. 那就是你要求dict 改变以前存储的值。

I hope my answer was clear enough :) 我希望我的回答很清楚:)

EDIT: format & language 编辑:格式和语言

In the python dictionary you cannot have duplicate keys. python dictionary您不能拥有重复的键。 If any duplicate key is present into the python dictionary , python automatically replaces the first values by the new ones. 如果python dictionary存在任何重复键, python自动将新值替换为第一个值。 python dictionary behaves as unique key. python dictionary表现为unique键。

In your example: 在你的例子中:

dict1 = {
  'A': ['A','A'],
  'AB':['A','B'], 
  'A':['A','O'], # 'A': ['A','A'] and 'A': ['A','O'] override.
  'B':['B','B'],
  'B':['B','O'], # 'B': ['B','B'] and 'B': ['B','O'] override.
  'O':['O','O']
}

Then your dictionary will be: 然后你的dictionary将是:

dict1 = {
  'A': ['A','O'],
  'AB':['A','B'], 
  'B':['B','O'],
  'O':['O','O']
}

I think, It will be helpfull . 我想,这将是有益的。

As Python Documentation says 正如Python文档所说

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). 最好将字典视为一组无序的键:值对,并要求键是唯一的(在一个字典中)。 A pair of braces creates an empty dictionary: {}. 一对大括号创建一个空字典:{}。 Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; 在括号内放置以逗号分隔的键:值对列表,将初始键:值对添加到字典中; this is also the way dictionaries are written on output. 这也是字典在输出上的写法。

The main operations on a dictionary are storing a value with some key and extracting the value given the key. 字典上的主要操作是使用某个键存储值并提取给定键的值。 It is also possible to delete a key:value pair with del. 也可以删除带有del的键:值对。 If you store using a key that is already in use, the old value associated with that key is forgotten. 如果使用已在使用的密钥进行存储,则会忘记与该密钥关联的旧值。 It is an error to extract a value using a non-existent key. 使用不存在的密钥提取值是错误的。

Reference : https://docs.python.org/3/tutorial/datastructures.html 参考: https//docs.python.org/3/tutorial/datastructures.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM