01 list of built-in method

''''''
'''
List:
    In [the], you can store a plurality of values ​​of any type, separated by commas.
    Hobbies are generally used for storing student, class periods, etc.
'' ' 
# Define a list of students, can be stored for more than one student 
# essentially a list ([' money Yao, '' Zhang day love ',' Xing Fei ',' Lin ']) 
Students = [ ' money Yao ' , ' Zhang day love ' , ' Xing Fei ' , ' Lin ' ]
 Print (Students [1 ])

student_info = [ ' I'm a ' ,. 19, ' Smale ' , [ ' slide ' , ' Dancing ' ]]
 # take'm a student all interested 
Print (student_info [. 3])   # [ 'slide', 'Dancing'] 
# take I'm a student of second hobby 
Print (student_info [3] [1])    # dancing


'''
Master priority actions:
    1, according to the index value (Forward + Reverse Access Access): also desirable to deposit
    2, a slice (care regardless of the end, step)
    3, the length len
    4, members and not in operation in
    5, append append (only appended.)
    6, delete del
    7, circulation
'' ' 
# By index values (Forward + Reverse Access Access): also desirable to deposit 
Print (student_info [-2]) # Smale

# Slice (care regardless of the end, step) 
Print (student_info [0:. 4: 2])   # [ 'I'm a', 'smale']

# Length len 
Print (len (student_info))     # . 4

# Members in operations in and not 
Print ( ' Lin '  in student_info) # True 
Print ( ' Lin '  not  in student_info) # False

# Append 
# append a value at the end of student_info list 
student_info.append ( ' Hefei ' )
 Print (student_info) # [ 'Lin', 19, 'smale', [ ' skateboard', 'Dancing'], 'Hefei' ]

# Delete 
# to delete the list index value of 2 
del student_info [2 ]
 Print (student_info) # [ 'Lin', 19, [ 'skateboard', 'Dancing'], 'Hefei']

# Need to know 
student_info = [ ' Lin ' , 19, ' Smale ' , [ ' skateboard ' , ' Dancing ' ], 19 ]
 # 1, get a list of index values 
Print (student_info.index (19))    # 1

# 2, COUNT get a list of the number of a value of 
Print (student_info.count (19))     # 2

# 3, POP values in parentheses are empty list of default to take the last value, similar to delete 
# If the pop () brackets write the index, the index takes a value corresponding to 
student_info.pop ()
 Print (student_info) # [ 'I'm a', 19 'Smale', [ 'slide', 'dancing']] 
student_info.pop (2 )
 Print (student_info) # [ 'I'm a', 19, [ 'slide', 'dancing']] 
# remove list index value of 2, and assigned to Sex 
student_info = [ ' I'm a ' , 19, ' Smale ' , [ ' slide ' , ' dancing ' ], 19 ]
Sex = student_info.pop (2 )
 Print (Sex)   # Smale 
Print (student_info) # [ 'I'm a', 19, [ 'slide', 'Dancing'], 19]

# 4 is removed, the value of a first value in the list is removed 
student_info.remove (. 19 )
 Print (student_info) # [ 'I'm a', [ 'slide', 'Dancing'],. 19] 

name student_info.remove = ( ' I'm a ' )
 Print (name) # None 
Print (student_info) # [[ 'slide', 'dancing'], 19]

# 5, interpolated values 
student_info = [ ' I'm a ' , 19, ' Smale ' , [ ' slide ' , ' Dancing ' ], 19 ]
 # in student_info, the position of the index 3 is inserted HEFEI 
student_info.insert (3, ' Hefei ' )
 Print (student_info) # [ 'Lin', 19, 'smale', ' Hefei' [ 'skateboard', 'dancing'], 19]

# . 6, Extend coalescing list 
student_info1 = [ ' I'm a ' , 19, ' Smale ' , [ ' slide ' , ' Dancing ' ], 19 ]
student_info2 = [ ' Xing phenanthrene ' , 20 is, ' Smale ' , [ ' slide ' , ' Dancing ' ]]
 # the value inserted into all student_info2 student_info1 
student_info1.extend (student_info2)
 Print (student_info1)     # [ 'I'm a' 19, 'Smale', [ 'slide', 'dancing'], 19, 'Xing phenanthrene', 20, 'smale', [ ' slide', 'dancing']]

#循环
for student in student_info:
    print(student)

 

Guess you like

Origin www.cnblogs.com/urassya/p/11083543.html