Chapter 3 python3, python3

Source: Internet
Author: User

Chapter 3 python3, python3

The Python dictionary contains the following built-in functions:

Serial number Functions and descriptions Instance
1 Len (dict)
Calculate the number of dictionary elements, that is, the total number of keys.
>>> dict = {'Name': 'cnblogs', 'Age': 7, 'Class': 'First'}>>> len(dict)3
2 Str (dict)
Output dictionary, which is a printable string.
>>> dict = {'Name': 'cnblogs', 'Age': 7, 'Class': 'First'}>>> str(dict)"{'Name': 'cnblogs', 'Class': 'First', 'Age': 7}"
3 Type (variable)
Return the input variable type. If the variable is a dictionary, the dictionary type is returned.
>>> dict = {'Name': 'cnblogs', 'Age': 7, 'Class': 'First'}>>> type(dict)<class 'dict'>

 

 

Serial number Functions and descriptions Instance
1 Radiansdict. clear ()
Delete all elements in the dictionary
>>> Dict = {'name': 'cnblogs', 'age': 7, 'class': 'first '}
>>> Dict. clear ()
>>> Dict
{}
2 Radiansdict. copy ()
Returns the shortest copy of a dictionary.
>>> Dict = {'name': 'cnblogs', 'age': 7, 'class': 'first '}
>>> D = dict. copy ()
>>> D
{'Name': 'cnblogs', 'age': 7, 'class': 'First '}
3 Radiansdict. fromkeys (seq [, value])
Create a new dictionary and use the elements in sequence seq as the dictionary key. val is the initial value corresponding to all the keys in the dictionary.
Seq = ('name', 'age', 'sex ')

Dict = dict. fromkeys (seq)
Print ("new dictionary: % s" % str (dict ))

Dict = dict. fromkeys (seq, 10)
Print ("new dictionary: % s" % str (dict ))


======= Output: ======
The new dictionary is {'name': None, 'age': None, 'sex': None}
The new dictionary is {'name': 10, 'age': 10, 'sex': 10}
4 Radiansdict. get (key, default = None)
Returns the value of the specified key. If the value is not in the dictionary, the default value is returned.

Dict = {'name': 'roy, 'age': 100}

Print ("Age value: % s" % dict. get ('age '))
Print ("Sex value: % s" % dict. get ('sex', "nan "))

 

======= Output: ======
Age: 100
Sex value: nan

 

5 Key in dict
If the key returns true in the dictionary dict, otherwise false is returned.

Dict = {'name': 'roy, 'age': 100}

# Check whether the key Age exists
If 'age' in dict:
Print ("key Age exists ")
Else:
Print ("key Age does not exist ")

# Check whether the Sex key exists
If 'sex' in dict:
Print ("key Sex exists ")
Else:
Print ("key Sex does not exist ")


======= Output: ======

Key Age exists
Key Sex does not exist

6 Radiansdict. items ()
Returns a list of traversal (Key, value) tuples.

Dict = {'name': 'roy, 'age': 100}

Print ("Value: % s" % dict. items ())


======= Output: ======

Value: dict_items ([('name', 'roy '), ('age', 100)])

7 Radiansdict. keys ()
Returns all keys of a dictionary in a list.

Dict = {'name': 'roy, 'age': 100}

Print ("all dictionary keys: % s" % dict. keys ())


======= Output: ======

All keys in the dictionary are dict_keys (['name', 'age']).

8 Radiansdict. setdefault (key, default = None)
Similar to get (), but if the key does not exist in the dictionary, a key is added and the value is set to default.

Dict = {'name': 'roy, 'age': 100}

Print ("Age key value: % s" % dict. setdefault ('age', None ))
Print ("Sex key value: % s" % dict. setdefault ('sex', None ))
Print ("new dictionary:", dict)

======= Output: ======

Age key value: 100
Sex key value: None
New Dictionary: {'name': 'roy ', 'age': 100, 'sex': None}

9 Radiansdict. update (dict2)
Update the dictionary dict2 key/value pairs to dict.

Dict = {'name': 'roy, 'age': 100}

Dict2 = {'sex ': 'female '}

Dict. update (dict2)
Print ("Update dictionary dict:", dict)


======= Output: ======

Update dictionary dict: {'name': 'roy ', 'age': 100, 'sex': 'female '}

10 Radiansdict. values ()
Returns all values in the dictionary as a list.

Dict = {'name': 'roy, 'age': 100}

Print ("all dictionary values:", list (dict. values ()))


======= Output: ======

All dictionary values: ['roy ', 100]

11 Pop (key [, default])
Delete the value corresponding to the specified key in the dictionary. The returned value is the deleted Value. The key value must be given. Otherwise, the default value is returned.

Dict = {'name': 'roy, 'age': 100}

Pop_obj = dict. pop ('name ')
Print (pop_obj)


======= Output: ======

Roy

12 Popitem ()
Returns and deletes a pair of keys and values in the dictionary randomly (generally, the end pair is deleted ).

Dict = {'name': 'roy ', 'age': 100, 'sex': 'nan '}

Pop_obj = dict. popitem ()
Print (pop_obj)
Print (dict)


======= Output: ======

('Sex', 'nan ')
{'Name': 'roy ', 'age': 100}

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.