Python list element deleted

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/SilentWu/article/details/72876722

Python list element deleted

1. Use the del command to delete the elements on the specified position in the list

l = [ . 1 , 2 , . 3 , . 4 , . 5 ]
 del l [ 2 ] The subscript element # 2 Delete
 Print ( l ) # [. 1, 2,. 4,. 5]
 del l # l delete the list at this time then print output is given as this list is not defined 
Print ( L ) #NameError: name 'L' IS not defined

[ . 1 , 2 , . 4 , . 5 ] 
Traceback (MOST Recent Last Call): 
  File "F.: / PythonDemo / Python Code / generating a random number and an odd remove .py" , Line 54 is , in < Module1 >
     Print (L) 
NameError : name 'L' IS Not defined

When to use the del command to delete the element cycle, it should be deleted in reverse order 

import random
def deleteoddnumber(x):
    for i in range(len(x) - 1, 0, -1): # 循环倒序
        if x[i] % 2 != 0:
            del x[i]
    return x
x = []
while True:
    if len(x) == 20:
        break
    n = random.randint(1, 100)
    if n not in x:
        x.append (n-) Print ( ' before removing the odd elements \ n- ' , X) 
 Print ( ' after deleting odd elements \ n- ' , deleteoddnumber (X))
Before deleting odd elements
  [ 24 , 19 , 71 , 2 , 83 , 1 , 6 , 45 , 29 , 38 , 78 , 74 , 46 , 14 , 93 , 48 , 100 , 31 , 25 , 30 ]
 after deleting odd elements
  [ 24 , 2 , 6 , 38 , 78 , 74 ,46, 14, 48, 100, 30]

If the positive sequence subscript out of range error occurs Delete

Import Random
 DEF deleteoddnumber (X):
     for I in Range ( len (X)): # cyclic positive sequence
         IF X [I]% 2 ! = 0 :
             del (X [I])
     return X
X = [] the while True :
     IF len (X) == 20 is :
         BREAK
 n-the random.randint = ( . 1 , 100 )
     IF n- Not in : X
        x.append (n-) Print ( ' before removing the odd elements
    
\ n- ' , X)
 Print ( ' after deleting odd elements \ n- ' , deleteoddnumber (X))
Before deleting odd elements
 Traceback (MOST Recent Call Last):
 [ 84 , 93 , 16 , 40 , 12 is , 98 , 67 , 97 , 83 , 39 , 26 is , 25 , 99 , . 19 , . 17 , 62 is , 68 , 43 is , . 4 , 47 ]
   File "F.: / pythonDemo / Python Code / generating a random number and an odd remove .py" , Line 15, In < Module1 >
 Print ( ' after deleting odd elements \ n- ' , deleteoddnumber (X))
   File "F.: / PythonDemo / Python Code / generating a random number and an odd remove .py" , Line . 4 , in deleteoddnumber
 IF X [ I]% 2 ! = 0 :
 IndexError: List index of OUT Range        

There will be a list index out of range error,

 2. List pop () method returns the element and remove the specified position (i.e., the default is the last pop (len (list) -1), remove the subscript index elements may be represented as POP (index))

Subscript index must not exceed the scope of the list, otherwise an exception is thrown.

L = [ . 1 , 2 , . 3 , . 4 , . 5 ]
 Print ( L .pop ( len ( L ) - . 1 )), and the same effect # l.pop () 
 Print ( L .pop ( . 3 ))

5
4
3. Use the list object remove () method removes the first occurrence of the specified element, if the element you want to delete the list does not exist, an exception is thrown.

L = [ 1 , 2 , 3 , 3 , . 4 , . 5 ]
 L .remove ( 1 ) # 1 to delete elements of the value of the element
 L .remove ( 3 ) deleting the element value element # 3 
 Print ( L )
[2, 3, 4, 5]
It is worth noting that when the remove method removes the first element will always find for deletion and not all the same element values ​​gave deleted.

Guess you like

Origin blog.csdn.net/SilentWu/article/details/72876722