7.3 List type and built-in method

List

Role: more equipment, more loving, and more courses, etc.

Definition: a plurality of any type can have the value of [], the comma separated

province_name_list = [ ' Beijing ' , ' Shanghai ' , ' Tianjin ' , ' Guangzhou ' , ' Shenzhen ' ] 
province_name_list1 = List ([ ' Beijing ' , ' shanghai ' , ' Tianjin ' , 5, ' Shenzhen ' ]) 

Print (province_name_list )   # [ 'Beijing', 'Shanghai', 'Tianjin', 'Guangzhou', 'Shenzhen'] 
Print (province_name_list1)   # [ 'Beijing', 'shanghai', 'Tianjin', 5, 'Shenzhen']

 

Need to know

1. Access the index value # Press (Forward + Reverse Access Access): may be taken to deposit

l = [1,2,3,4]
print(l[0:4:1])  # [1, 2, 3, 4]
print(l[0::])  # [1, 2, 3, 4]
print(l[5::-1])  # [4, 3, 2, 1]
print(id(l))  # 4508301640
l[0] = 69
print(id(l))  # 4508301640
print(l)  # [69, 2, 3, 4]

2. Slice # (care regardless of the end, step)

province_name_list = [ ' Beijing ' , ' Shanghai ' , ' Tianjin ' , ' Guangzhou ' , ' Shenzhen ' ] 

Print (province_name_list [1: 4])   # [ 'Shanghai', 'Tianjin', 'Guangzhou'] 
Print (province_name_list [ -2:])   # [ 'Guangzhou', 'Shenzhen']

# 3. length

l = [11,22,33,44,55]
print(len(l))
>>>
5

# 4 members and not in operation in

l = [11,22,33,44,55]
print( 444 in l)
>>>
False

# 5. Add the elements (# Key #) to the list

  append

# # Tail add a 66 
L = [11,22,33,44,55 ] 
l.append ( 66 )
 Print (L)
 >>> 
[ . 11, 22 is, 33 is, 44 is, 55, 66]

  insert

# # Add any position of the element 
L = [11,22,33,44,55 ] 
l.insert ( 2,96)   # additive element at an arbitrary position by the index 
Print (L)   # Note insert data values can be added as for a list element 
>>> 
[ 11, 22, 96, 33, 44, 55]

  extend

# # Replenishing container type data 
L = [11,22,33,44,55 ] 
L1 = [99,88,77,66 ] 
l.append (L1) 
l.insert ( -1 , L1) 
l.extend (L1 )   # internal principle for one loop l1 appended to the tail of the list 
l.extend ([. 1 ,])
 Print (L)
 >>> 
[ . 11, 22 is, 33 is, 44 is, 55, [99, 88, 77, 66] , [99, 88, 77, 66], 99, 88, 77, 66, 1]

# 6. Delete

Del # 
L = [11,22,33,44,55 ] Print (L) # [. 11, 22 is, 33 is, 44 is, 55] del L [2] # del delete operations for all Print (L) # [ 11, 22, 44, 55]
# POP 
L = [11,22,33,44,55 
RES1 = l.pop ()   # tail pop 
RES2 = l.pop () 
RES3 = l.pop ()
 Print (RES1, RES2, RES3)   # 55 44 is 33 is 

L = [11,22,33,44,55 ] 
RES1 = l.pop (0)   # specify the index by index pop-up element 
Print (RES1). 11 #
# Remove 
L = [11,22,33,44,55 ] 
RES = l.remove (33 is)   # specify the value to remove elements 
Print (L)   # [. 11, 22 is, 44 is, 55] 
Print (RES)   # None

# 7 cycles

l = [11,22,33,44,55]
for i in l:
    print(i)
>>>
11
22
33
44
55

Guess you like

Origin www.cnblogs.com/PowerTips/p/11129662.html