Python Learning Note 4-python dictionary tuples

Source: Internet
Author: User

Definition of a dictionary

Wrap with curly braces {}, separate each key-value pair with a comma, connect the key to the value with a colon, make up the (key{values) structure.

Keys and values can be arbitrary data objects, most of which are composed of numbers and strings

Dictionaries are unordered, keys must be unique in dictionaries, and values in dictionaries are searched by key for corresponding values

Dictionary is a mapping data type in Python

Dictionaries do not support stitching (concatenation) and repetition (repetirion)

a={} #定义空字典, without any keys and values print aa={' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4} #创建字典, the key corresponds to the value, separated by commas, the key and the value are concatenated with a colon to print aa={}.fromkeys (the list (' ABCdef ') #创建字典, using the dictionary method, the value creation key, the value is None, the key comes from the list of elements of print aa=dict ([' X ', 1], [' Y ', 2])) #使用字典函数创建字典print aa=dict (Zip (["Key1", ' Key2 ]) #使用zip方法和dict函数创建字典print A

Execution results are

{} {' A ': 1, ' C ': 3, ' B ': 2, ' d ': 4}{' a ': none, ' C ': None, ' e ': None, ' d ': none, ' F ': none}{' y ': 2, ' x ': 1}{' Key2 ': 2, ' Key1 ': 1}

Zip ([iterable, ...])

Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples. If the length of the passed parameter is not equal, the length of the returned list is the same as the object with the shortest length in the parameter. Using the * operator, you can unzip the list (unzip).

>>> a=zip (List (' ABCDEFG '), List (' 1234567 ')) >>> print a[(' A ', ' 1 '), (' B ', ' 2 '), (' C ', ' 3 '), (' d ', ' 4 '), (  ' E ', ' 5 '), (' F ', ' 6 '), (' G ', ' 7 ')]>>> Zip (*a) [(' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '), (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ')]

Second, dictionary access

Dictionaries are unordered, and the order in which dictionaries are defined is different from the order in which they are generated.

Gets the value corresponding to the dictionary key by the dictionary key

>>> a={' A ': 1, ' B ': 2, ' C ': {' d ': 3}} #字典中包含字典 >>> print a{' A ': 1, ' C ': {' d ': 3}, ' B ': 2}>>> a[' a ']  1>>> a[' C ']{' d ': 3}>>> a[' C ' [' d ']3>>> a[' a ']=67 #字典更新 >>> a{' a ': ", ' C ': {' d ': 3}, ' B ': 2}>>> del a[' a '] #字典删除 >>> a{' C ': {' d ': 3}, ' B ': 2}

Three, the Dictionary method

1.dict.keys () gets all the keys in the dictionary and returns them as a list

Dict.values () gets all values in the dictionary, returned as a list

>>> a=dict (List (' abcdef '), List (' 1234567 ')) >>> a{' a ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' F ': ' 6 '}>>> a.keys () [' A ', ' C ', ' B ', ' e ', ' d ', ' F ']>>> A.keys () [3] ' E ' >>> a.values () [' 1 ', ' 3 ', ' 2 ', ' 5 ', ' 4 ', ' 6 ']>>> a.values () [3] ' 5 '

2.dict.pop (K,[d])

POPs the value corresponding to the specified key K, and deletes the key and its corresponding value, if the key is not found, returns the specified d

>>> a{' A ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' F ': ' 6 '}>>> a.pop (' C ') ' 3 ' >>> a{' a ': ' 1 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' F ': ' 6 '}>>> a.pop (' C ', ' not found ') ' isn't found ' >>> a.pop (' d ', ' not found ') ) ' 4 '

3.dict.update ()

Updates the key and value in the dictionary, and the key name, even if it is a string, does not need to be quoted when updating, and the update is done in an assignment. Equivalent to dict[' key ']= ' New-value '

>>> a{' A ': ' 1 ', ' B ': ' 2 ', ' E ': ' 5 ', ' F ': ' 6 '}>>> a.update (a=55) >>> a{' A ':, ' B ': ' 2 ', ' E ': ' 5 ', ' F ': ' 6 '}>>> a[' a ']=66>>> a{' a ':, ' B ': ' 2 ', ' E ': ' 5 ', ' F ': ' 6 '}

4.dict.get () Gets the value corresponding to all keys in the dictionary, same as d[' KeyName ')

>>> a{' A ':}>>>, ' B ': ' 2 ', ' E ': ' 5 ', ' F ': ' 6 ' a.get (' a ') 66>>> a[' a ']66

5.dict.items ()

Place the key and value of the dictionary in a tuple, with each key and value as a tuple, placed in the list to store the returned

>>> a{' A ':}>>>, ' B ': ' 2 ', ' E ': ' 5 ', ' F ': ' 6 ' a.items (') [(' A ', ' 2 '), (' E ', ' 5 '), (' F ', ' 6 ')]

6.dict.popitem () randomly deletes a key-value pair from the dictionary and returns it as a tuple

>>> a{' A ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 ', ' I ': ' 9 ', ' H ': ' 8 ', ' J ': ' 0 '}>> > A.popitem (' A ', ' 1 ') >>> A.popitem () (' C ', ' 3 ') >>> A.popitem () (' B ', ' 2 ') >>> A.popitem ( ) (' E ', ' 5 ')

7.dict.viewitems () returns the element view of the dictionary Dict

Dict.viewkeys () returns the key view of the dictionary Dict

Dict.viewvalues () Returns the value view of the dictionary Dict

>>> a{' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 ', ' I ': ' 9 ', ' H ': ' 8 ', ' J ': ' 0 '}>>> a.viewitems () dict_items ([' d ', ' 4 ')  , (' G ', ' 7 '), (' F ', ' 6 '), (' I ', ' 9 '), (' H ', ' 8 '), (' J ', ' 0 ')]) >>> A.viewkeys () Dict_keys ([' d ', ' G ', ' f ', ' I ', ' H ', ' J ']) >>> a.viewvalues () dict_values ([' 4 ', ' 7 ', ' 6 ', ' 9 ', ' 8 ', ' 0 '])

8.dict.iteritems () Returns the iteration object of the dictionary Dict

Dict.iterkeys () Returns the iteration object of the dictionary dict key

Dict.itervalues () Returns the value of the Dictionary dict iteration object

>>> a{' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 ', ' I ': ' 9 ', ' H ': ' 8 ', ' J ': ' 0 '}>>> a.iteritems () <dictionary-itemite Rator object at 0x0000000002e2fa98>>>> A.iterkeys () <dictionary-keyiterator object at 0x0000000002e41548>>>> a.itervalues () <dictionary-valueiterator object at 0x0000000002e2fa98>> >> A.iteritems (). Next (' d ', ' 4 ') >>> A.iterkeys (). Next () ' d ' >>> a.itervalues (). Next () ' 4 '

9.dict.setdefault (K,[d])

If the key is in the dictionary, the value corresponding to the dictionary key is returned, and if the key is not created, the value defaults to none, if D is given a value of D

>>> a{' d ':  ' 4 ',  ' G ':  ' 7 ',  ' F ':  ' 6 ',  ' I ':  ' 9 ',  ' h ':  ' 8 ',  ' j ':  ' 0 '}>>> a.setdefault (' d ',) ' 4 ' > >> a.setdefault (' a ')      #创建键a >>> a{' a ': none,  ' d ':   ' 4 ',  ' G ':  ' 7 ',  ' F ':  ' 6 ',  ' I ':  ' 9 ',  ' h ':  ' 8 ',  ' j ':  ' 0 '} >>> a.setdefault (' A ', *)      #存在键a, no longer created, A has a value of none, >>> a{' A ' is not displayed:  None,  ' d ':  ' 4 ',  ' G ':  ' 7 ',  ' F ':  ' 6 ',  ' I ':  ' 9 ',  ' h ':  ' 8 ' ,  ' j ':  ' 0 '}>>> a.setdefault (' B ', K ')      #创建键b, and assigned value 88, will 88 pop 88> >> a{' A ': none,  ' B ': 88,  ' d ':  ' 4 ',  ' G ':  ' 7 ',  ' F ':  ' 6 ',   ' I ':  ' 9 ',  ' h ':  ' 8 ',  ' j ':  ' 0 '} 

10.dict.copy () produce a copy of a dictionary, or an alias.

A=dict (Zip (list (' ABCDEFG '), List (' 123456789 '))) b=a.copy () print aprint bprint B is ab[' a ']=99print bprint a

Execution results are

{' A ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 '} {' A ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 '} False{' A ':, ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 '} {' A ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 '}
Import copya=list (' ABCDEFG ') b=copy.copy (a) print Aprint b

You can use this method to copy other objects, such as lists, and execute the result as

[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '] [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']

11.dict.clear () Clears all keys, values in the dictionary

>>> a{' A ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 '}>>> a.clear () >>> a{}

12.dict.has Key ()

Member relationship Check to determine if the specified key exists in the dictionary

PYTHON3.0 has key will not be supported, replaced by in

>>> a{' A ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' E ': ' 5 ', ' d ': ' 4 ', ' G ': ' 7 ', ' F ': ' 6 '}>>> a.has_key (' a ') true>>  ;> a.has_key (' h ') false>>> ' a ' in atrue>>> ' a ' not in afalse>>> ' h ' in afalse>>> ' H ' Not in Atrue

Iv. python tuples

Python tuples are important data objects for Python

In most cases, tuples are not modifiable for system input or system returns, but support iterations

The tuple approach is relatively small, with only count and index

A tuple is defined as an object that contains "," as a split element, and an element can be any data object.


V. Python data type comparison

Python data types are flexible, with lists, dictionaries, tuples

Compare points
List
Dictionary Meta-group
Representation method
[],[1,2] {},{' a ': 1, ' B ': 2} (), (ON)
Ways to access elements
Index Key Index
Order of
Ordered
Disordered Ordered
Variability
Variable Variable Not variable
operability
Operation Rich Operation Rich Monotonous operation

This article is from the "Raffaele" blog, make sure to keep this source http://raffaele.blog.51cto.com/6508076/1569599

Python Learning Note 4-python dictionary tuples

Related Article

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.