Python: why dictionary type data can automatically exclude certain elements?

I created a dictionary containing six elements as shown 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 find that the elements "{'A': ['A', 'A'], 'B': ['B', 'B']}"

have been excluded.

>>> 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 excluded elements. It becomes normal.

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

      

Can anyone explain to me why?

+3


source to share


4 answers


You cannot have duplicate keys, but you can have multiple values. In other words, each key is unique.

This way, every time you assign new values โ€‹โ€‹to the same key, you override the previous key values.

A way to assign 2 values (or lists) as in your example could be as follows:

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 like this:

dict1['A']

      

Result

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

      

This is similar to what you want to do.

Hope it helps.

+3


source


The thing with Python dictionaries is that each key is unique . That is, when you add an existing entry, the previous saved value is overwritten with the new one.

When typing:

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 have given the dictionary two meanings for the keys 'A'

and 'B'

. It is you asked the dict to change the previously saved value.

Hope my answer was clear enough :)

EDIT: format and language

+1


source


As python dictionary

you can not be a duplicate keys. If any duplicate key is present in python dictionary

, python

automatically replaces the first values โ€‹โ€‹with new ones. python dictionary

behaves like 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 yours dictionary

would be:

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

      

I think it will be helpful.

+1


source


As the Python documentation says

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys be unique (within the same dictionary). A pair of curly braces creates an empty dictionary: {}. Placing comma separated lists of keys: value pairs in curly braces adds initial key: values โ€‹โ€‹pairs to the dictionary; it is also a way to write dictionaries in the output.

Basic operations in a dictionary are storing a value using some key and retrieving the value given the key. You can also remove the key: value pair with del. If you store a key that is already in use, the old value associated with that key will be forgotten. It is an error to retrieve a value using a non-existent key.

Link: https://docs.python.org/3/tutorial/datastructures.html

+1


source







All Articles