I. column list
Introduction 1.1 List
Is a list of the python ⼀ of a base data type, other programming languages have similar data types such as arrays of JS, java in the array, etc. It is surrounded by [], with each element ',' spaced on it and can store various types of data:
= LST [ 1 , ' ha ' , ' Houhou " , [ 1 , 8 , 0 , " Baidu " ], ( " I " , " name " , " element " , " group " ), " ABC " , { " my name " : " dict dictionary " }, { " I called the collection " , "Set " }]
Compared to a list of strings not only can store different types of data on it and can store large amounts of data can be stored python 32:... 536 870 912 elements 64 can be stored: 1152921504606846975 elements on it and the list is ordered ( order you save), an index, you can cut still pictures ⽅ value.
II. List of additions and deletions to change search
1. increase
Note, list and str are not the same. Lst can change so directly motivated ⾏ operating in the original object
By # LST = [ ' date ' , ' as ' , ' fragrance ' , ' LU ' , ' green ' , ' purple ' , ' smoke ' ] lst.append ( ' Bai ' ) print(lst) lst.insert ( 1 , ' lens ' ) in the lens insertion position # 1, the original position of the element moves rearwardly a print(lst) # Add iterative lst.extend ([[ ' Liu ' , ' Xuan ' , ' de ' ]]) print(lst)
2. Delete pop, remove, clear, del
# Delete LST = [ ' date ' , ' as ' , ' fragrance ' , ' LU ' , ' green ' , ' purple ' , ' smoke ' ] EL = lst.pop ( 3 ) # default deletion removed the last element No. 3 is returned to remove elements print (el) lst.clear () # Clear List print(lst) lst.remove('紫') print(lst) # Cut pieces delete still pictures del lst[1:3] print(lst)
3. Modify Index modify sections
Modify Index # LST = [ ' King Jan ' , ' king of February ' , ' King of March ' , ' King of April ' ] LST [ 0 ] = ' daji ' print(lst) LST [ . 3 ] = ' Zhen Ji ' print(lst) # Slice modification LST = [ ' King Jan ' , ' king of February ' , ' King of March ' , ' King of April ' ] LST [ . 1 : . 3 ] = ' Luban ' , ' Sun Bin ' Print (LST)
4. query, a list is iterable, so it can be a for loop
for el in lst: print (el)
5. Other operations
Sort # lst = [1,2,3,4,7,8,44,44,33,99,0,6] lst.sort () # ascending order, from small to large order lst.sort (Reverse = True) # reverse, descending order reversed (lst) # reverse, a reverse order according to the original location print(lst)
# Test string length LST = [ ' King Jan ' , ' king of February ' , ' King of March ' , ' King of April ' ] lst = len(lst) print(lst) # Queries king appeared in January c = lst.count ( " King Jan " ) print(c)
IV. Nested tuple and the tuple
Tuple: a list of commonly known immutable separate warranty is called read-only list, the basic data type of tuples of python is ⼀, ⼩ Use parentheses, ⾥ surface may put any data type, the query may be recycled. slice can. but that can not be changed.
Note: Do not change the tuple element means Submenu Submenu immutable elements within ⽽ Submenu element can be changed, depending on whether the sub-elements are mutable object.
Tuple if only ⼀ elements. Be sure to add a comma, otherwise it is not a tuple
tu = ( 1 ,) print(type(tu))
Five. Range
range can help us get to a set of data. These data can be obtained through the for loop.
# Range count for i in the Range ( 10 ): # Only one argument, starting at 0, 10 ends print(i) for I in Range ( 5 , 10 ): when two parameters #, starting at 5, 10 ends print(i) for I in Range ( 9 - 10 - 2 ): # three parameters, beginning 9 - 10 ends, step 2, from the right to left print(i)