简体   繁体   中英

How can I remove this element from the list?

There are total 9 elements which contains the name of drinks. What I need to do is remove any elements that contains "Americano"

However, I am not able to debug my code and it keep shows an error. How can I remove list1's element [0,2,3,6] which contains "Americano"?

list1 = [ ['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'], ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]

while True:
        if " Americano" in list1[i]:
            del list1[0]
    if i < x:
        i = i + 1
        continue

    if i >= x:
        break

print(list1)

Use a list comprehension to filter out the unwanted elements.

list1 = [x for x in list1 if 'Americano' not in x]

Results:

>>> list1 = [ ['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'], ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
>>> 
>>> 
>>> list1 = [x for x in list1 if 'Americano' not in x]
>>> list1
[['4', 'Smoothie_queen', '4', '12000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
>>> 

using filter function, this will create a new list and make result

list1 = [ ['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'], ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
result =list(filter(lambda x: 'Americano' not in x , list1))
print(result)

output

[['4', 'Smoothie_queen', '4', '12000'],
 ['6', 'Americano', '17', '34000'],
 ['7', 'Cafe_mocha', '4', '11200'],
 ['8', 'Cafe_latte', '11', '27500'],
 ['10', 'Amorparty', '2', '4000'],
 ['11', 'Plain_yogurt', '13', '45500']]

using enumerate and list.remove() function, on existing list it modify it accordingly without creating new list

list1 = [ ['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'], ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
for index, value in enumerate(list1):
    if 'Americano' in value:
        list1.remove(value)

print(list1)    

output

[['4', 'Smoothie_queen', '4', '12000'],
 ['6', 'Americano', '17', '34000'],
 ['7', 'Cafe_mocha', '4', '11200'],
 ['8', 'Cafe_latte', '11', '27500'],
 ['10', 'Amorparty', '2', '4000'],
 ['11', 'Plain_yogurt', '13', '45500']]

if you don't want to be confused Here is Step by Step code

list1 = [['3', 'Americano', '7', '14000'], ['4', 'Smoothie_queen', '4', '12000'], ['5', 'Americano', '2', '4000'],
         ['6', 'Americano', '17', '34000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'],
         ['9', 'Americano', '17', '34000'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]
lis = []
for row in list1:
    if 'Americano' not in row:
        lis.append(row)
print lis

Output

[['4', 'Smoothie_queen', '4', '12000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM