First, the dictionary1, the initialization of the dictionaryA dictionary is a key-value structurein[160]:d={}in[161]:type (d) Out[161]: dictin[166]:d={' A ':1, ' B ': 2}in[167]:dout[167]:{' a ': 1, ' B ': 2}in[180]:d=dict ({"A":0, "B": 1}) in[181]:dout[181]: {' A ':0, ' B ': 1}in[164]:d=dict ([["A", 1],["B", 2] ]) # Th
(dict['Jack'])#this way, if key does not exist, it will be an error .6 7 Print('Jack' inchDICT1)#determine if Jack is in this dictionary, returns TRUE or falseBuilt-in methods1Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 3 Print(Dict1.keys ())#Print all keys4 5 Print(Dict1.values ())#Print all value6
, the result of the output:Ainfo = {' ab ': ' liming ', ' ac ': ' Sex ': ' Man ', ' Age ': 20}>>> ainfo = {' ab ': ' liming ', ' AC ': 20}>>> ainfo[' sex ']= ' man '>>> ainfo[' age ']=20>>> Ainfo{' AC ': ' AB ': ' liming ', ' age ': ' + ' sex ': ' Man '}>>> ainfo.update ({' Sex ': ' Man ', ' Age ': 20})>>> Ainfo{' AC ': ' AB ': ' liming ', ' age ': ' + ' sex ': ' Man '}2 output result: [' ab ', ' AC ']>>> Ainfo.keys () [0:
= Set1.difference (Set2)#difference = minus public part-Set1-set2 Print(set3) Set3= Set1.symmetric_difference (Set2)#symmetry difference = set-intersection ^ Set1 ^ Set2 Print(SET3) forValinchSet2:Print(val)Print(Set2.pop ())#you can get one, there's no guarantee what you got. Print(Set2)if3inchSet2:set2.remove (3)#Judging, deleting elements Print(Set2) Set4= {1, 2} Print(Set4.issubset (Set1))#4 is a subset of 1, Set4 Print(Set1.iss
DescribeThe Python Dictionary copy () method returns a shallow copy of a dictionary (parent invariant, child change).GrammarCopy () method syntax:Dict.copy ()Parameters
NA.
return valueReturns a shallow copy of a dictionary (the parent does not change, the child changes).InstanceThe following example show
Keys for dictionaries
There is no limit to the values in the dictionary, which can be any Python object, from standard objects to user-defined objects, but the keys in the dictionary are type-restricted.(1) A key is not allowed to correspond to multiple valuesOne principle must be clarified: Each key can only correspond to one item. That is, it is not allowed to
It is often used in development to traverse the dictionary, list, and other data cyclically. However, in python, dictionary traversal is very unfamiliar to many beginners, today, we will talk about the circular traversal of dictionary and list data in python, but the
This article mainly introduces how to convert a list into a dictionary data structure in Python, and analyzes the related techniques of converting Python numeric data types in the form of examples, for more information about how to convert a list in Python to a dictionary da
A dictionary is a data structure of python, such as a set of mappings in mathematics, that can be understood as a list of elements that exist by key and value pairs.A. Dictionary construction form:1. Dictionary name = {Key Name 1: value 1, key Name 2: Value
1. Dictionary creationThe dictionary is separated by a colon between each key and its value, and the items are separated by commas, and the entire dictionary is surrounded by a pair of curly braces. An empty dictionary does not include any item {}.2. Dict functionLike LIST,T
3, Del d[' a '] #删除键值为 ' a ' element
Traversing elements
For K in D:
print ' d[%s]= '% k,d[k]
Or
For k,v in D.items ():
print ' d[%s]= '% k,v
Or
For k,v in D.iteritems ():
print ' d[%s]= '% k,v
Or
For k,v in D.viewitems ():
print ' d[%s]= '% k,v
Items (), Iteritems () and viewitems () differences
Python2.x's items () is a list that contains all the elements of dict, like the one above, but because of this too much memory, it was later added (note: The beginning of
The code is as follows
Copy Code
#!/usr/bin/env python#-*-Encoding:utf-8-*-Dict2 = {}A = ' a 'Dict2.setdefault (a,1) #加入一个键和值 a,1A = ' B 'Dict2.setdefault (a,1) #加入一个键和值 b,1A = ' a 'Dict2.setdefault (a,2) #加入一个键和值 a,1, the resulting duplicate key cannot be repeated by adding {' A ': 1, ' B ': 1}Print Dict2#遍历key和valueFor key in Dict2.keys ():print ' key ', key, ' value ', Dict2[key]Print Dic
2. dictionary-to-model and xib, iosxib for iOS developmentI. dictionary-to-model (plist file)
Model encapsulation method: (put in the code repository)
1. Add the property name corresponding to the key value of the plist file to the. h file.
Array --- > NSArrayDictionary --- > NSDictionarystring ---> NSStringnumber ---> NSNumber
Note: The property name and the key
present in the dictionary, it is assigned a value by Dict[key]=default.
Dict.setdefault (Key,default=none)
Similar to the method set (), if the key is not present in the dictionary, it is assigned a value by Dict[key]=default.
Practice:
Shopping Cart Program requirements:
1. After starting the program, let the user enter the salary, and then print the list of goods;
DictionarydictionaryPython uses {} or dict() creates an empty dictionary:{} dict()Type(a DictAfter you have dict, you can add elements to them by using the index key values, or you can view the values of the elements by index:Insert key value:a["one"] = "This is number 1" a["both"] = "This was number 2" a{' one ': ' This is number 1 ', ' both ': ' This is number
; dict1 {'foo': 'xyz'} >>> dict1['foo'] = 123 >>> dict1 {'foo': 123}
(2) the key must be hash-able.
Most Python objects can be used as keys, but they must be hash objects. Variable types such as lists and dictionaries cannot be used as keys because they are not hash-able.All unchangeable types can be hashed, so they can be used as dictionary keys. It sh
Python converts string and dictionary types.
This document describes how to convert string and dictionary types in Python by using examples. The specific method is as follows:
1. convert a dictionary (dict) to a string (string)
We can easily convert the
and key is not in the dictionary, a KeyError is raised. d. popitem () Remove and return an arbitrary (key, value) pair fro M the dictionary. d. setdefault (key [, default]) If key is in the dictionary, return its value. if not, insert key with a value of default and return default. default ults to None. d. update (B) Update the
Python has three built-in data structures: list, ancestor, and Dictionary. This article introduces these three data structures and provides examples, let's take a closer look at the differences between the three.
1. list)
List is the data structure that processes a group of ordered projects. you can store a series project in a list.
The items in the list should be included in square brackets so that
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.