A Dictionary of Python learning

Source: Internet
Author: User
Tags shallow copy

#coding: Utf-8
__author__ = ' Administrator '
From copy import deepcopy
#字典由多个键及其对应的值构建的对组成, the key is unique and the value is not unique
phonebook={' Lyq ': 6589, ' Ming ': 9878, ' Tong ': 4586}
#1. dict function, which can be used to build a dictionary through other mappings (such as other dictionaries) or sequence pairs (keys, values)
item=[(' nn ', ' BB '), (' age ', 89)]
D=dict (item)
Print D
Dd=dict (name= ' Gaoli ', age=42)
Print DD

#字典的基本操作
#1. Len (d) Returns the number of image (key-value) pairs in D
Print Len (DD)
#3. D[k] Returns the value associated to K
Print dd[' name ']
#4. D[k]=v associates The value V to the key K
dd[' name ']= ' Meimei '
Print DD
#5. del D[k] Delete items with key k
Del dd[' name ']
Print DD
#6. K in D checks if there are any items in D that contain a key of K
If ' age ' in D:
Print d[' age ']

#字典格式化
Print "Lyq ' s phone number is% (LYQ) s." %phonebook

#字典方法
#1. Clear method clears all items in the dictionary, no return value for in-place operation
d={}
d={' Mali ': 78}
D.clear ()
Print D
#2. Copy: Returns a new dictionary with the same key-value pair (this method implements a shallow copy because the value itself is the same, not a copy)
#副本替换值原字典不变, delete and add original dictionary changes
d={' Mali ': +, ' Shasha ': [' foo ', ' bar ', ' Que ']}
Dy=d.copy ()
dy[' Mali ']=45
dy[' Shasha '].remove (' bar ')
dy[' Shasha '].append (' KKK ')
print ' dy ', dy
print ' d ', D
#3. deepcopy deep Copy, copy all of the values it contains
d={' Mali ': +, ' Shasha ': [' foo ', ' bar ', ' Que ']}
Dy=deepcopy (d)
dy[' Mali ']=45
dy[' Shasha '].remove (' bar ')
print ' dy ', dy
print ' d ', D
#4. Fromkeys is used to create a new dictionary for a given key, and each key defaults to a value of none, or to a specified value
li=[' Mali ', ' Lyq ']
Mm=dict.fromkeys (LI)
print mm
Mm=dict.fromkeys (Li, ' JJ ')
print mm
#5. Get method is a looser way to access a dictionary entry, and accessing a nonexistent item will get a value of none (or the value you set)
d={' Mali ': +, ' Shasha ': [' foo ', ' bar ', ' Que ']}
Print d[' Mali ']
# print d[' KK ' ERROR
Print D.get (' KK ')
Print D.get (' KK ', ' JJ ')
#6. Has_key method can check whether the dictionary contains the given key, there is a return True,python 3.0 does not have the function
print ' Has_key ', D.has_key (' Mali ')
#7 the. Items method returns all dictionary items as a list, each of which comes from (key, value)
d={' Mali ': +, ' Shasha ': 85}
Print D.items ()
The #iteritems method works roughly the same, but returns an iterator object instead of a list
It=d.iteritems ()
Print it
Print List (IT)
#8 the. Keys method returns the keys in the dictionary as a list, and Iterkeys returns an iterator for the key
Print D.keys ()
I=d.iterkeys ()
Print I
Print List (i)
#9. The pop method is used to get the value corresponding to the given key and then remove the key-value pair from the dictionary
d={' Mali ': +, ' Shasha ': 85}
D.pop (' Mali ')
Print D
The #popitem method is similar to List.pop, which pops up the last element of the list, but the difference is that the popitem pops up as a random item, because there is no "last element" or other notion of book ordering.
d={' Mali ': +, ' Shasha ': 85}
D.popitem ()
Print D
#10. The SetDefault method is somewhat similar to the Get method, which is the ability to obtain a value associated with a given key, in addition to which the SetDefault can set the corresponding key value without a given key in the dictionary.
d={}
D.setdefault ("name", ' N/A ')
Print D
d[' name ']= ' Ali '
D.setdefault (' name ', ' N/A ')
Print D
#当键不存在时, SetDefault returns the default value and the corresponding update dictionary, and returns its corresponding value if the key exists, but does not change the dictionary
#11. The Update method can update another dictionary with one dictionary item
d={' title ': ' Python Web site ', ' url ': ' www.python.com ', ' changed ': ' Mar '}
x={' title ': ' Python language web site '}
D.update (x)
Print D
# values and Itervalues methods return a value in the dictionary as a list (Itervalues returns an iterator), and the list of returned keys does not hurt, the list of returned values can contain duplicate elements
d={}
D[1]=1
D[2]=3
d[3]=2
D[4]=1
Print d.values ()

A Dictionary of Python learning

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.