Python notes--dictionaries

Source: Internet
Author: User

Dictionary dict

Dictionaries are stored using key-value (key-value) keys, which must be different from one another in a dictionary.

The dictionary has a very fast speed, because the use of key-value storage, when put in the key to calculate the vlaue location, so that key can be directly located to the value .

The sequence is indexed by successive integers, and unlike dictionaries, the dictionary is indexed by a keyword, which can be any immutable object (not modifiable), usually a string or numeric value, which also guarantees that the storage location of value does not change.

Since a key can only correspond to one value, the value is placed on a key more than once, and subsequent values flush out the previous value.

It takes a lot of memory, and it wastes a lot of memory.

First, Defining Dictionaries

>>> name={' Zhangsan ': All, ' Lisi ':, ' Wangwu ': 70}

With curly braces, keys and values are separated by colons, and multiple key-values are separated by commas, and a key can only correspond to one value.

Second, Basic Operations

#返回所有键值

>>> name.items (' Wangwu ', 70) [(' Lisi ', '), (' Zhangsan ', ' + ')]

#返回所有键

>>> Name.keys () [' Lisi ', ' Zhangsan ', ' Wangwu ']

#返回所有值

>>> name.values () [80, 90, 70]

Note: The results returned above are in the form of a list

#添加键值

>>> name[' Zhaoliu ']=60>>> name{' Lisi ': $, ' Zhaoliu ': $, ' Zhangsan ': $, ' Wangwu ': 70}

#获取单个键的值

>>> name[' Zhangsan ']90

Note: If this key does not exist, it will output keyerror error

>>> name.get (' Zhangsan ') 90

Note: If this key does not exist, the output is empty.

#删除指定指定的键值

>>> name.pop (' Zhangsan ') 90>>> name{' Lisi ': $, ' Zhaoliu ': $, ' Wangwu ': 70}

#删除第一个键值

>>> name.popitem (' Lisi ', +) >>> name{' Zhaoliu ': $, ' Wangwu ': 70}

#把其他字典的键值添加到本字典中

>>> name{' Zhaoliu ': $, ' Wangwu ': 70}>>> name1={' Jerry ': A, ' Mike ':66}>>> name.update ( name1) >>> name{' Mike ': 70, ' Zhaoliu ': $, ' Jerry ': "Wangwu

#拷贝为一个新的字典

>>> name2=name.copy () >>> name2{' Mike ': 70, ' Zhaoliu ': $, ' Jerry ': "Wangwu

Note: name2 does not change with name

>>> name3=name>>> name3{' Mike ': 70, ' Zhaoliu ': $, ' Jerry ':, ' Wangwu

Note: The Name3 will change with name

#判断字典中是否有这个键

>>> name.has_key (' Wangwu ') true>>> name.has_key (' FJC ') False

#如果字典中有这个键, the corresponding value is returned, not the key is added to the dictionary, the value defaults to None

>>> name{' Mike ': The ' Zhaoliu ': $, ' Jerry ': The ' Wangwu ': 70}>>> name.setdefault (' Mike ') 66>> > Name.setdefault (' Mike ', 66>>> Name.setdefault (' Mike '), 66>>> name{' Mike ': ' Zhaoliu ': ' Jerry ': ' Wangwu ': 70}>>> name.setdefault (' FJC ') >>> name{' Mike ': The ' FJC ': None, ' Zhaoliu ': 60 , ' Jerry ': ' Wangwu ': 70}>>> name.setdefault (' Miss ', ') ' 50>>> name{' Mike ': The ' FJC ': None, ' Zhaoliu ' ': ' Miss ': ' Jerry ': ' Wangwu ': 70}

Note: The parentheses in. SetDefault () can contain both the key and the value.

#根据一个列表生成字典中的键, the value is none

>>> lst=[1,2,3,4,5]>>> Dic={}.fromkeys (LST) >>> Dic{1:none, 2:none, 3:none, 4:none, 5:none}

#清空字典

>>> dic.clear () >>> dic{}

#字典迭代器

. Iteritems () # Get all key values

. Iterkeys () # Get all keys

. Itervalues () # Get all values

>>> a=name.iteritems () >>> a.next (' Mike ', 66)

Note: multiple executions. Next () to list key values

#一个键多个值

is to change the value to a list, a tuple, or a dictionary

>>> b={' a ': [2], ' B ':2}>>> c={' a ':(, ' B ':2}>>> d={' a ': {' C ': 3, ' d ': 4}, ' B ':}

This article is from the "Network Technology" blog, please be sure to keep this source http://fengjicheng.blog.51cto.com/11891287/1927625

Python notes--dictionaries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.