Data Types - list

List

create

# Way a l1 = [1,2, 'taibai' ]

# Second way l2 = list ( 'fdhkjdakgnkagankjf')

Print (L1, L2) # will be an element within a string are spaced apart [ 'f', 'd' , 'h', 'k', 'j', 'd', 'a', 'k' , 'g', 'n' , 'k', 'a', 'g', 'a', 'n', 'k', 'j', 'f']

# Three ways: list comprehensions

CRUD

= L3 [ ' Taibai ' , ' Egon ' , ' xiaofeng ' , ' Yanlong ' ] Print (len (L3))

increase

append: Add

. 1 l3.append ( ' XX ' ) Print (L3) # Example: entering new Employee Name 
2  
. 3   the while . 1 :
 . 4  . 5   name = INPUT ( " Please enter the employee performance: (Q q or quit the program) ' )
 . 6 . 7 IF name.upper () == ' Q ' : BREAK . 8 . 9 l3.append (name)
 10 . 11 Print (L3)   
     

insert insert

1  l3.insert(2,'xxx')
2  print(l3)

#extend with additional iterations

l3.extend ( ' ABCD ' ) # string character smallest element 
 l3.extend ([ ' Alex ' , 1,2]) # list within the list of elements as the minimum element 
 Print (L3)

delete

remove

1  # Delete accordance element 
2  
. 3   l3.remove ( ' Egon ' )
 . 4   Print (L3)

pop

# According to the index delete 

 l3.pop ( -2) # POP index is not specified, the default delete the last 
 Print (L3)

clear

# Empty elements in a list

 l3.clear()
 print(l3)

of the

1   # remove by index 
2  
. 3   del L3 [-1 ]
 . 4   Print (L3)
 . 5  
. 6  # deleted according to step a slice 
. 7  
. 8   del L3 [:: 2 ]
 . 9   Print (L3)

change

1   # according to the index change value 
2  
. 3   L3 [0] = ' nanshen ' 
. 4   Print (L3)
 . 5  
. 6  # accordance slice change value (understanding) 
. 7  
. 8   L3 [1:] = ' fkkdfaj ' 
. 9   Print (L3)
 10  
. 11  # according slice (step Learn) 
12 is  
13 is   L3 [:: 2] = ' ABC ' 
14   Print (L3)

check

1  for i in l3:
2      print(i)

 



Guess you like

Origin www.cnblogs.com/zyiy/p/12500830.html