The list of basic data types

List list:

Usage: used to store a plurality of values

Definitions: definitions in [] brackets, a plurality of values ​​separated by a comma, the value may be any type

Conversion Type: 

Whenever loop can be traversed for the data types can be passed list () is converted into a list of type, list () will be the same for each element traversing the data type contained in the for loop is then placed with the list
 >>> List ( ' wdad ' ) # result: [ 'W', 'D', 'A', 'D'] 
>>> List ([l, 2,3]) # results: [1, 2, 3] 
> List >> ({ " name " : " Jason " , " Age " : 18 is}) # results: [ 'name', 'Age'] 
>>> List ((l, 2,3)) # results: [1 , 2, 3] 
>>> List ({1,2,3,4}) # results: [1, 2, 3, 4]
Common Operation:
 # 1, the index can take positive and negative values :( can preferably also stored) 
USER_INFO = [ ' Egon ' , 18 is, [ ' Football ' , ' EAT ' , ' SLEEP ' ]]
 Print (USER_INFO [ 2 ] [0])
 >>> Football 

# 2, index slice 
my_friends = [ ' Tony ' , ' Jason ' , ' Tom ' ,. 4 ]
 Print (my_friends [0:. 4: 2])    # third parameter represents step 
>>> [ 'tony', ' Tom ' ] 

# . 3, the length len (): Get a list of the number of elements 
my_friends = [ ' Tony ' , ' Jason ' , ' Tom ' ,. 4 ] 
len (my_friends)
 >>>. 4 # . 4, operation members and in in Not 
my_friends = [ ' Tony ' , ' Jason ' , ' Tom ' ,. 4 ]
 ' Tony ' in my_friends
 >>> True
 ' Egon 'in my_friends


  >>> False 

# . 5, loop 
# looping through the list which my_friends value 
for I in my_friends:
     Print (I) 
 ' Tony ' 
' Jack ' 
' Jason ' 
. 4 

built-in method: 
# 1, Add 
# the append (): Append end of the list value, a one-time only add value 
L1 = [ ' a ' , ' B ' , ' C ' ] 
l1.append ( ' D ' )
 Print (L1)
 >>> [ 'a', ' B ' , ' C ' , ' D ' ] 

# INSERT (): the value is inserted by the inserting position specified by the index 
L1 = [ ' A ' , ' B ' , ' C ' ] 
l1.insert (0, " First " )   # 0 represents the index position by interpolation 
Print (L1)
 >>> [ ' First ' , ' A ' , ' B ' , ' C ']

#extend (): adding a plurality of disposable elements in the end of the list 
L1 = [ ' A ' , ' B ' , ' C ' ] 
l1.extend ([ ' A ' , ' B ' , ' C ' ])
 Print (L1)
 >>> [ ' A ' , ' B ' , ' C ' ,   ' A ' , ' B ' , ' C '] 

# 2, delete
# 2.1 the Remove (): Removes the specified value, can only delete a no return value 
L = [11,22,33,22,44 ] 
RES = l.remove (22) # from left to right to find the first brackets need to remove elements 
Print (RES)
 >>> None
 # 2.2 POP () default start delete from the list the last element, and returns the value delete, delete elements can be specified by adding the index in brackets 
l = [11,22 , 33,22,44 ] 
RES = l.pop ()
 Print (RES)
 >>> 44 is 

RES = l.pop (. 1 )
 Print (RES)
 >>> 22 is # 2.3 del completely remove the 
l = [11,22, 33, 44 ]
 del L [2]   # deleted index elemental Print (L)
 >>> [11,22,44 ] #




3, reverse () reverse the order of elements in the list 
L = [11,22,33,44 ] 
l.reverse () 
Print (L)
 >>> [44,33,22,11 ] 

# . 4, Sort () to the list ordering all the elements within 
# listing 4.1 ordering between the elements must be the same data type, not mix, otherwise an error 
L = [11,22,3,42,7,55 ] 
l.sort () 
Print (L)
 >>> [3, 7, 11, 22, 42, 55]   # default in ascending order 

L = [11,22,3,42,7,55 ] 
l.sort (Reverse = True)   # Reverse specifying whether sorting fall the default is False 
Print (L)
 >>> [55, 42 is, 22 is,. 11,. 7,. 3 ] 

# . 5, the sorted (): Python built-in function, to generate a new list sorting, the same original data 

# 6 , count (): the number of specified elements in the current list
= L [11,22,3,42,7,55,11 ]
 Print (l.count (. 11 ))
 >>> 2 # . 7, index (): Get the current value of the index of the specified element, you can also specify the Look 
= L [11,22,3,42,7,55,11 ]
 Print (l.index (11,1,7 ))
 >>>. 6 # . 8, Clear (): Clear list data 
l = [11,22 , 3,42,7,55,11 ] 
l.clear () Print (L)
 >>> [] 
supplementary knowledge: # queue: FIFO               
L1 = []                                  
l1.append ( . 1 ) 
l1.append ( 2 ) 
l1.append ( . 3 )
 Print (L1) 
l1.pop (0)







Print (L1) 
l1.pop (0) 
l1.pop (0) 
Print (L1) 

# Stack: after the advanced 
L1 = [] 
l1.append ( . 1 ) 
l1.append ( 2 ) 
l1.append ( . 3 )
 Print ( L1) 
l1.pop () 
Print (L1) 
l1.pop () 
l1.pop () 
Print (L1)

 

 

Guess you like

Origin www.cnblogs.com/baohanblog/p/11807400.html