python in practice in list

   List: 
1. by
  1.1 append, adding elements to the end of the list, using the method: list.append ( 'element')   
li = ['alex', 'wusir', 'eric', 'rain', 'alex']
li.append('seven')
print(li)
#运行结果['alex', 'wusir', 'eric', 'rain', 'alex', 'seven']
  1.2 insert, append the element by index position, use: list.insert (index position, 'element')
li = ['alex', 'wusir', 'eric', 'rain', 'alex']
li.insert(0, 'seven')
print(li)
#运行结果:['seven', 'alex', 'wusir', 'eric', 'rain', 'alex']
 
  1.3 extend, additional iterations, an additional set of data in the end of the list, using the method: list.extend ( 'element')
= Li [ ' Alex ' , ' wusir ' , ' Eric ' , ' Rain ' , ' Alex ' ] 
li.extend ( ' Seven ' )
 Print (Li)
 # Run Results: [ 'alex', 'wusir ', 'eric ',' rain ',' alex ',' s', 'e', 'v', 'e', 'n']
 

2. deleted

  2.1 remove, delete list by the first element of the same elements, use: list.remove ( 'element')

li = ['alex', 'wusir', 'eric', 'rain', 'alex']
li.remove('alex')
print(li)
#运行结果:['wusir', 'eric', 'rain', 'alex']

  2.2 pop, by deleting the index position corresponding to the element, has a return value, the return value of the element removed, use: list.pop (index position)

li = ['alex', 'wusir', 'eric', 'rain', 'alex']
dle = li.pop(2)
print(dle, li)
#运行结果:eric ['alex', 'wusir', 'rain', 'alex']

  2.3 clear, empty list, using the method: list.clear ()

= Li [ ' Alex ' , ' wusir ' , ' Eric ' , ' Rain ' , ' Alex ' ] 
li.clear () 
Print (Li)
 # result: []

  2.4 del, according to an index, a slice, a slice (step), the element deletion list

= Li [ ' Alex ' , ' wusir ' , ' Eric ' , ' Rain ' , ' Alex ' ] 

# del 
# 1. Remove the element indexed by 
del Li [0]
 Print (Li)
 # Run Results: [ 'wusir', 'Eric', 'Rain', 'Alex'] 

# 2. remove elements by dicing 
del Li [:. 3 ]
 Print (Li)
 # run results: [ 'Rain', 'Alex'] 

# 3. by dicing (step ) Removing elements 
del li [: 3:2 ]
 print (to)
 #Run Results: [ 'wusir', 'rain', 'alex']

3. change

= Li [ ' Alex ' , ' wusir ' , ' Eric ' , ' Rain ' , ' Alex ' ] 

# 1. index change according to 
Li [0] = ' Zhu ' 
Print (Li)
 # Run Results: [ 'zhu', 'wusir', 'Eric', 'Rain', 'Alex'] 

# 2. slice change according to iteratively increase 
Li [:. 4] = ' Love ' 
Print (Li)
 # run results: [ 'l', 'o ',' V ',' E ',' Alex '] 

# . 3.According slice (step) changes, must correspond (not corresponding to the error) 
Li [:: 2] = ' ABC '
Print (Li)
 # Run Results: [ 'a', 'wusir ', 'b', 'rain', 'c']

4. Charles

= Li [ ' Alex ' , ' wusir ' , ' Eric ' , ' Rain ' , ' Alex ' ] 

# 1. index search 
Print (Li [2 ])
 # Run Results: Eric 

# 2. Slice check 
Print (Li [: 2 ])
 # run results: [ 'Alex', 'wusir'] 

# 3. sections (steps) check 
Print (Li [:: 2 ])
 # run results: [ 'alex', 'eric ', 'alex' ] 

# 4 by cyclic query 
for I in Li:
    print(i)

5. Other operations

count (number) (number of times an element statistics appear in the list), use: list.count ( 'element')

index (an index to find the location of a process for a value of the first match from the list), the method using: list.index ( 'element')

 

= Li [. 5,. 7, 12 is, 15,. 1,. 1 ] 

Print (li.count (. 1 ))
 # result: 2 

Print (li.index (. 1 ))
 # result: 4
View Code

 

sort (in situ method for the list in a forward direction), using the method: list.sort ()

list.sort (reverse = True) (in situ method is used to reverse the order of the list)

 

= Li [. 5,. 7, 12 is, 15,. 1 ] 

li.sort () 
Print (Li)
 # Run Results: [. 1,. 5,. 7, 12 is, 15] 

li.sort (Reverse = True)
 Print (Li)
 # run results: [15, 12, 7, 5, 1]
View Code

 

reverse (the method of storage elements in the list reverse), using the method: list.reverse ()

len (method of measuring the length of the list elements), using the method: len (list)

 

= Li [. 5,. 7, 12 is, 15,. 1,. 1 ] 

li.reverse () 
Print (Li)
 # Run Results: [. 1,. 1, 15, 12 is,. 7,. 5] 

Print (len (Li))
 # Run results: 6
View Code

 

Guess you like

Origin www.cnblogs.com/zhuzl/p/11117115.html