Fourth class set, the dictionary, the operator

A, set
  1, set of characteristics
    disorder: a disordered array elements within the
    uniqueness: the inner elements are unique, like elements will deduplication
  2, creating a collection of

= {1,2,3,4,5} SE 
SE = SET ([1,2,3,4,5]) # into iterables 

se = set () # Create an empty set 
outputs are: 
{ 1,2,3,4,5}

  3, the set of operations
    and set: | Intersection: & set difference: - with non-set: ^

= {l, 2,3} SE1; SE2 = {2,3,4} 
SE1 | SE2 = {1,2,3,4} and set #: go is to add weight 
se1 & se2 = {2,3} # intersection : both overlapping portions 
se1-se2 = {1} # difference set: SE1 are not part se2 
^ se2 = {1,4} # set with non SE1: both of the opposite part of the supplementary, and the intersection

  4, the operation of the method of collection

se.add () # add a single element 
se.update () # add more elements, Xu seemed iterables 
se.remove () # remove an element specified 
se.pop () # throws a random element

 

Second, the dictionary (the only mapping type)
  1, the characteristics of the dictionary
  key is unique, duplicate key will be re-copied (definitions)
  disorder
  2, dictionary creation

di = { 'a': 123 , 'b': 456, 'c': 789} # internal elements as long as the key to form 
di = dict (a = 123, b = 456, c = 789) # key definition subject to variable naming rules

  3, values ​​and modify the dictionary

= {DI 'A': 123, 'B': 456, 'C':} 789 
DI [ 'A'] is an input value # button 
di [ 'a'] = 321 # may cover define, i.e. modify

  4, the operation method of the dictionary

di.fromkeys ([ 'd', ' e'], 111) # Create a new dictionary with the given key, each key default value None (can be customized, such as 111) 
di.copy () Copy # a dictionary 
di.clear () # empty dictionary a 

DI = { 'a': 123, 'B': 456, 'C': 789} 
di.get ( 'F', 'not me') eject key # 'F value ', if there is a corresponding value is returned, otherwise the default value None (can be customized, as I do not) 

di.items () # View all items 
di.keys () # View all the key 
di.values ( ) # View all the values 
to which a particular query (key, value), you need to dictionary into a list, and then index, query value method can also be get, and there are pop, setdefault such as 
li = list ( di.items ()) 

di.pop ( 'd', 'I'm not') # thrown specified key value pairs, if not, return to the custom character (must be customized) 
di.pop () # random deleted a key-value pair (item) 

di.setdefault ( 'e', 222) is similar to # get, buttons of 'e', if present, its value is returned to, otherwise updating the dictionary for the default value is worth None ( can be customized, such as 222) 

DI2 = { 'A': 333, 'D': 444}
di.update (DI2) word # di2 add content, and updating the dictionary to the original cover di
Output: { 'a':

 

Third, the operator

** exponentiation # 
+ - * /% # arithmetic operators: addition, subtraction modulo 
<> <=> = # comparison operators: less than greater than or less than or equal to 
== # = comparison operators: not equal to equal! 
= % = / = - = + = * = * = # assignment operator: equal to taking more than equal to the addition equal to minus equal plus take equal equal equal power 
is is not # identity operator: is not 
in not in # member operator: inside not there 
not> and> or # logical operators 

and (to) two conditions are satisfied # returns True 
or (or) a # conditions returns True 
not (non) # negated 

For chestnut: 
a. 1 = ; b = 2 
A == B ==. 3. 1 and # Not according to the logical operator, not higher priority than and
Output: True

 

Fourth, work

  1, there are two lists x = [1,2,3, 'a', 'b', 'c'] y = [ 'a', 'b', 'c'] to find the list of x in y and some elements

z = set (x) & set (y) 
output: { 'a', 'b ', 'c'}

  2, a new dictionary, inserting values ​​into the dictionary in three ways; values ​​taken by the four methods, taken in 2 ways key

插入: 
a = { 'a': 1, 'b': 2, 'c': 3} 
of [ 'd'] = 4 
di.setdefault ( 'and', 5) 
2 = dict (f = 6); di.update (2) 

取键: 
li3 = list (di.keys ()); f = li3 [0] 
and LI1 = [0] [0] 
x = di.popitem (); x [0] 

取值: 
of [ 'a']                
di.pop ( 'a')            
x = di.popitem (); x [1] 
di.get ( 'a') 
di.setdefault ( 'b') 
LI1 = list (di.items ( )); c = LI1 [0] [1] 
LI2 = list (di.values ()); d = LI2 [1]

  3, the definition of each type of data we have learned, and state, which is variable, which is immutable

#int type. 1 = A (A) 
B = 1.2 #float 
C = True #bool 
D = + 2J. 1 # complex-type 

query keyword method: 
Method a: Import keyword 
           keyword.kwlist 
Method two: help ( 'keywords') 

s = 'abc' #str s = "abc" s = '' 'abc' '' three definitions methods 
Li. [1,2] #list 
TU = (1,2) = 1,2 #tuple TU 

SE = #set 1,2} { 
DI = { 'X':. 1, 'Y': 2} #dict 

List and dict variable, none of the remaining variable, by looking at whether or not the variable complex-type id

 

Guess you like

Origin www.cnblogs.com/sysun110/p/11267570.html