共有9个元素,其中包含饮料的名称。我需要做的是删除所有包含“ Americano”的元素
但是,我无法调试我的代码,并且一直显示错误。如何删除包含“ Americano”的list1的元素[0,2,3,6]?
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)
答案 0 :(得分:4)
使用列表推导过滤掉不需要的元素。
@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
//fetch new list(if any) of departments added
List<Department> theDepartments = departmentService.getDepartments();
//Create a linkedhash map to hold our department_id-department name information
final LinkedHashMap<Integer, String> departmentOptions = departmentService.generateDepartmentOptions(theDepartments);
// create new employee object and attach to our model atrribute.
//how to add multiple objects?? doing this so i can pre-populate available departments for selection
theModel.addAttribute("employee", departmentOptions);
Employee theEmployee = new Employee();
//how to add multiple objects?? doing this so when program return control to controller it will help me set the attribute of employees so I can save it into the database
theModel.addAttribute("employee", theEmployee);
return "customer-form";
}
结果:
list1 = [x for x in list1 if 'Americano' not in x]
答案 1 :(得分:1)
使用filter
函数,这将创建一个新列表并产生结果
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)
输出
[['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']]
使用enumerate
和list.remove()
函数,在现有列表上对其进行修改
因此无需创建新列表
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)
输出
[['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']]
答案 2 :(得分:0)
如果您不想感到困惑,这是分步代码
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
输出
[['4', 'Smoothie_queen', '4', '12000'], ['7', 'Cafe_mocha', '4', '11200'], ['8', 'Cafe_latte', '11', '27500'], ['10', 'Amorparty', '2', '4000'], ['11', 'Plain_yogurt', '13', '45500']]