python in Ganso dictionary

Ganso's introduction

The python tuples and a list of similar, except that the ancestral element can not be modified . Ganso use parentheses, square brackets list

 

 

 

<1> access tuples                                     execution results:

 

 

<2> modified tuples                                         execution result:

  

Description: python tuples are not allowed to modify the data, including the value of which can not be deleted

Built-in functions count <3> of tuples, index

index and count the string and use the same list

Example: Result:

              

 

 

Introduction dictionary

Think: 
If there is a listing
nameList = [ 'xiaoZhang', ' xiaoWang', 'xiaoLi'];
the need for "xiaoWang" wrong name, by modifying the code:
nameList [. 1] = 'xiaoxiaoWang'
if the order of the list change, the following
nameList = [ 'xiaoWang', ' xiaoZhang', 'xiaoLi'];
in this case it is necessary to modify the standard, modified in order to complete the name
nameList [0] = 'xiaoxiaoWang'
there a way, both storing a plurality data, but also in access to elements easily be able to locate the elements that need it?
A: The
dictionary
Another scenario: 
student information list, each student information, including number, name, age, a student of how to find information?

 <1> Life dictionary

    

 

 

 <2> Software Development Dictionary

Variable info is dictionaries: 
info = { 'name': 'monitor', 'id': 100, 'sex': 'f', 'address':' Earth Asia Beijing China '}
Description:' name ':' squad '----> key-value pairs
dictionaries and lists, but also be able to store more data
in the list when looking for an element, is conducted according to the following standard
when looking for an element dictionary, is based on' name '(that is, colon : front of that value, for example, the above code 'name', 'id', 'sex')
for each element of the dictionary is composed of two parts, a key (key): value (value). For example, 'name': 'monitor', 'name' is a bond (key), 'monitor' the value (value)

 <3> The access key value

 

 

 

   

Assumptions: If the access key does not exist, what happens then?

 

 

 

 

 

    

Think about it: When a key is if we are not sure whether there is a dictionary and want to get their value, 
how does it work?
Answer: You can use the get () method.

 

 

 

  

Think about it: If the 'age' of this key does not exist info, I just let it return the default value of 18, 
can operate?
A: can operate! ! !

 

 

 

    

 

 

Do exercise common dictionary

<1> modifying element 
data for each element in the dictionary can be modified, as long as found by the key, can be modified

 

 

 

  

<2> additive element
Access the absence of elements

    

 

 

    

If you use the variable name [ 'key'] = data, the "key" in the dictionary, does not exist, it will add this element

  

 

 

     

<4> Remove elements 
of the dictionary deletion There are three kinds:
(1) del
(2) the Clear () ---- Clear
del delete the specified element ( can not access after deleting, otherwise it will error )

       

 del delete an entire dictionary

                  

 clear clear the entire dictionary

                     

 

 

 

Dictionary of common operations (upgrade)

<1> len () 
measured in the dictionary, the number of key-value pairs

           

 

 

   

<2> keys 
returns a list of all dictionary KEY comprising

 

 

 

   

<3> values 
returns a list of all the value of the dictionary contains

 

 

 

   

<4> items 
returns a list of all the (key, value) Ganso

  

 

 

Common traversal operation

Traversed 
by for ... in ... we can traverse the strings, lists, tuples, dictionaries, etc.
indents attention python syntax
string traversal

   

Through the list of

 

Ganso traversal

   

Traversing the dictionary 
traversing the dictionary key (key)

   

Traversing the dictionary value (value)

    

Traversing the dictionary items (elements)

   

items traversing dictionary (key-value pairs)

    

Think about it: 
how to traverse the subscripted index?

    

How to traverse a subscript index?

the enumerate () 
the enumerate () function is used to traverse a data object (such as a list, string, or tuples) as a combination index sequence, while the data lists and data standard, which is generally used in a for loop.

    

 

 

Introduction of a collection

Sets are unordered collection of elements are unique , the set of tuples for general or list of elements to weight. 

Define an empty set
setl = SET ()

# note the following lines to an empty dictionary
set2 = {}
Additive element (the Add, Update) 
the Add
setl = {. 1, 2,. 4,. 5}
# additive element
set1.add (. 8)

Update
setl = {. 1, 2,. 4,. 5}
# is passed to the element to be split , as the individual passed to the collection
set1.update ( "abcd")
Remove elements (remove, POP, discard) 
remove
setl = {. 1, 2,. 4,. 5}
# remove use in deleting the collection element if delete programs if no error
set1.remove (22 is)

POP
setl = {. 1, 2 , 4, 5}
# remove pop used to delete a random element in the set if no element set1 say given program
set1.pop ()
discard
setl = {. 1, 2,. 4,. 5}

# remove use directly remove discard no action if the element is not present if the element is present
set1.discard (2)  

Intersection of the sets and union

Intersection and union (& and |) 
intersection
set1 = {. 1, 2,. 3,. 4}
SET2 = {. 3,. 4,. 5,. 6}
new_set = set1 & SET2
Print (new_set)
# {. 3,. 4}
and set
set1 {. 1 =, 2,. 3,. 4}
SET2 = {. 3,. 4,. 5,. 6}
new_set = setl | SET2
Print (new_set)
# {. 1, 2,. 3,. 4,. 5,. 6}

Public method

Operators

 

 

Public method (+)

        

 

 

Public method (*)

Public method (in) (Note that, in operation when the dictionary, the dictionary key determination)

 

 

python built-in functions

Python contains the following built-in functions

    

len () Note: len in operation dictionary data, returns the number of key-value pairs.

     

max()

     

del 
del in two ways, one is del spaces, the other is the del ()

      

Guess you like

Origin www.cnblogs.com/jiaxinzhu/p/11785098.html