Here we use two examples to analyze how to use python to delete the list content. We also provide ideas to achieve this through pop and remove methods, respectively, for more information, see. A little busy today
a=['XXXX_game.sql', 'XXXX_game_sp.sql', 'XXXX_gamelog_sp.sql', 'XXXX_gamelog.sql']for i in a: if 'gamelog' in i: a.remove(i)print a['XXXX_game.sql', 'XXXX_game_sp.sql', 'XXXX_gamelog.sql']
The 'xxxx _ gamelog. SQL 'project was obviously missing during the review process. You can try it on your own. Why is it not deleted? Why?
We verify again
for i in a: if 'gamelog' in i: print i, XXXX_gamelog_sp.sql XXXX_gamelog.sql
If we don't perform the remove operation on the result, there will be no problem. It can be fully experienced.
In this way, we will probably know that it is impossible to use the calendar method to remove the list. So how to solve it?
A1 = a [:] # Here we mirror a list a1, but never use a1 = a. Why can we test a1 = a [:]? a1 = a True; a1 is a False; If a1 = a a1 = a True; a1 is a True, you can test it. This is a feature of the list. For I in a1: if 'gamelog 'in I: a. remove (I) print a ['xxxx _ game. SQL', 'xxxx _ game_sp. SQL ']
Another example
[{'Num': '001', 'name': 'zhang san', 'workingtime': 'monday', 'money': '123 '}
{'Num': '002 ', 'name': 'lily', 'workingtime': 'tuesday', 'money': '000000'}]
Because 'zhang san' exists, delete {'num': '001', 'name': 'zhang san', 'workingtime': 'monday', 'money ': '000000'} how to operate the entire row
The idea is to find the index of the element to be deleted in the list, and then call pop, the index as the parameter. Pop returns the deleted element. The rest of the queue is the rest after the index element is deleted.
Lname = [{'num': '001', 'name': 'zhang san', 'workingtime': 'monday', 'money ': '000000'} {'num': '002', 'name': 'lily', 'workingtime': 'tuesday', 'money ': '200'}] for x in range (len (lname): # list traversal if l [x] ['name'] = u 'zhangsan': lname. pop (x) # Use pop. Break # after the operation is completed, break goes out
Now, we will be here today.