python - 如何遍历列表并搜索多个列表
问题描述
这些是杂货店清单:
aisle_1 = ['cereals', 'cocoa', 'grits', 'honey', 'oats', 'syrup', 'tea']
aisle_2 = ['apple sauce','canned fruits', 'dried fruits', 'canned juices', 'canned meats', 'prunes', 'raisins', 'canned seafood', 'soup' ]
aisle_3 = ['baking soda', 'cake mixes', 'extracts', 'flour', 'gelatins', 'ketchup', 'mayonnaise', 'meal', 'mustard', 'oils', 'olives', 'pickles','pie crust','salad dressing', 'salt', 'spices', 'sugar', 'vinegar', 'yeast' ]
aisle_4 = ['international foods', 'macaroni', 'canned mushrooms', 'rice', 'meat sauces', 'dried spaghetti', 'tomato paste', 'canned vegetables']
aisle_5 = ['cookies', 'crackers','nuts', 'popcorn', 'potato chips']
aisle_6 = ['matches', 'toilet paper', 'tissue', 'soft drinks', 'toothpicks']
aisle_7 = ['ammonia', 'bleaches', 'brooms', 'mops', 'candles', 'disinfectants', 'insectisides', 'moth balls', 'paper towels','napkins','paper plates', 'soap-detergents', 'waxes']
aisle_8 = ['dog food', 'light bulbs', 'pet foods', 'toys/games']
aisle_9 = ['baby needs', 'bar soap', 'batteries']
aisle_10 = ['frozen meats', 'frozen meals', 'water']
aisle_11 = ['frozen breakfast', 'frozen bread']
aisle_12 = ['beer', 'ice cream', 'frozen yogurt', 'popsicles', 'ice']
aisle_13 = ['bread', 'candy', 'dairy products', 'jellies', 'margarine', 'marshmallows', 'peanut butter']
rear_left= ['eggs', 'milk', 'yogurt', 'sour cream']
rear_right = ['gluten free']
produce = ['vegetables']
我所要做的就是输入客户想要购买的东西,我已经通过这个循环做到了:
while True:
shoppinglist = input('enter a grocery item you need:')
if shoppinglist.lower() == 'quit':
break
if shoppinglist.lower() in grocery_items:
grocery_list.append(shoppinglist)
else:
print('that item is unavailable')
我还已经为grocery_list 发送了空列表,并且有一个名为Grocery_items 的列表,它只是将上面的每个列表与+ 组合在一起。最后,我为每个过道设置了另一个空列表。前任。a1 = [] a2 = []。
我的问题是我必须创建一个遍历购物清单的 FOR 循环,并且该循环必须将列表中的每个项目添加到每个过道的空变量中。例如, aisle_1 包含蜂蜜,因此如果grocery_list 包含蜂蜜,则应将其添加到空列表a1 中。
这就是我想出的:
for item in grocery_list:
if item.lower in aisle_1:
a1.append(item)
if item.lower in aisle_2:
a2.append(item)
if item.lower in aisle_3:
a3.append(item)
if item.lower in aisle_4:
a4.append(item)
if item.lower in aisle_5:
a5.append(item)
if item.lower in aisle_6:
a6.append(item)
if item.lower in aisle_7:
a7.append(item)
if item.lower in aisle_8:
a8.append(item)
if item.lower in aisle_9:
a9.append(item)
if item.lower in aisle_10:
a10.append(item)
if item.lower in aisle_11:
a11.append(item)
if item.lower in aisle_12:
a12.append(item)
if item.lower in aisle_13:
a13.append(item)
if item.lower in rear_left:
rear_l.append(item)
if item.lower in rear_right:
rear_r.append(item)
if item.lower in produce:
prod.append(item)
出于某种原因,这整件事被忽略了,当我打印一个应该更改的列表时,它只是显示为空。例如,用户输入蜂蜜和沙粒,在 For 循环中,它应该遍历列表并看到蜂蜜在通道一并将其添加到 a1,然后看到沙粒也在通道一并将其添加到 a1。我不知道为什么它没有将它添加到每个列表中。
我目前拥有的最终代码:
aisle_1 = ['cereals', 'cocoa', 'grits', 'honey', 'oats', 'syrup', 'tea']
aisle_2 = ['apple sauce','canned fruits', 'dried fruits', 'canned juices', 'canned meats', 'prunes', 'raisins', 'canned seafood', 'soup' ]
aisle_3 = ['baking soda', 'cake mixes', 'extracts', 'flour', 'gelatins', 'ketchup', 'mayonnaise', 'meal', 'mustard', 'oils', 'olives', 'pickles','pie crust','salad dressing', 'salt', 'spices', 'sugar', 'vinegar', 'yeast' ]
aisle_4 = ['international foods', 'macaroni', 'canned mushrooms', 'rice', 'meat sauces', 'dried spaghetti', 'tomato paste', 'canned vegetables']
aisle_5 = ['cookies', 'crackers','nuts', 'popcorn', 'potato chips']
aisle_6 = ['matches', 'toilet paper', 'tissue', 'soft drinks', 'toothpicks']
aisle_7 = ['ammonia', 'bleaches', 'brooms', 'mops', 'candles', 'disinfectants', 'insectisides', 'moth balls', 'paper towels','napkins','paper plates', 'soap-detergents', 'waxes']
aisle_8 = ['dog food', 'light bulbs', 'pet foods', 'toys/games']
aisle_9 = ['baby needs', 'bar soap', 'batteries']
aisle_10 = ['frozen meats', 'frozen meals', 'water']
aisle_11 = ['frozen breakfast', 'frozen bread']
aisle_12 = ['beer', 'ice cream', 'frozen yogurt', 'popsicles', 'ice']
aisle_13 = ['bread', 'candy', 'dairy products', 'jellies', 'margarine', 'marshmallows', 'peanut butter']
rear_left= ['eggs', 'milk', 'yogurt', 'sour cream']
rear_right = ['gluten free']
produce = ['vegetables']
#Define the empty lists or variables you may need.
#You will need an empty list for the user inputed grocery list
#Also a blank list for each aisle that corresponds to the aisles above. This is where your grocery list items will be separated by aisle.
#You will also need a list that combines the items from the aisles above into one big list.
grocery_list = []
a1 = []
a2 = []
a3 = []
a4 = []
a5 = []
a6 = []
a7 = []
a8 = []
a9 = []
a10 = []
a11 = []
a12 = []
a13= []
rear_r = []
rear_l = []
prod = []
grocery_items = aisle_1 + aisle_2 + aisle_3 + aisle_4 + aisle_5 + aisle_6 + aisle_7 + aisle_8 + aisle_9 + aisle_10 + aisle_11 + aisle_12 + aisle_13 + rear_right + rear_left + produce
#Construct a while loop that takes user input
#If the user enters "quit" stop the loop
#If the grocery item is available in the store, add it to your grocery list
#If a item is not available in the store, print a message stating it is unavailable
while True:
shoppinglist = input('enter a grocery item you need:')
if shoppinglist.lower() == 'quit':
break
if shoppinglist.lower() in grocery_items:
grocery_list.append(shoppinglist)
else:
print('that item is unavailable')
#Print your entire grocery list
print('This is your grocery list: ', grocery_list)
#Construct a for loop that iterates through the grocery list
#Each item in the grocery list should be added to a blank list that corresponds to an aisle from the store
for item in grocery_list:
if item.lower in aisle_1:
a1.append(item)
if item.lower in aisle_2:
a2.append(item)
if item.lower in aisle_3:
a3.append(item)
if item.lower in aisle_4:
a4.append(item)
if item.lower in aisle_5:
a5.append(item)
if item.lower in aisle_6:
a6.append(item)
if item.lower in aisle_7:
a7.append(item)
if item.lower in aisle_8:
a8.append(item)
if item.lower in aisle_9:
a9.append(item)
if item.lower in aisle_10:
a10.append(item)
if item.lower in aisle_11:
a11.append(item)
if item.lower in aisle_12:
a12.append(item)
if item.lower in aisle_13:
a13.append(item)
if item.lower in rear_left:
rear_l.append(item)
if item.lower in rear_right:
rear_r.append(item)
if item.lower in produce:
prod.append(item)
#Print the aisle names and the grocery list items on each aisle
#dont worry about this part ik how to do this
最后,如果我添加打印 a1,而不是打印应该在过道一的杂货,它只打印一个空白。前任。[]
解决方案
确保您使用的是 item.lower() 而不是 item.lower。我还会使用字典,其中键是过道的名称,值是该过道中的项目列表。
inventory = dict()
inventory['aisle_1'] = ['cereals', 'cocoa', 'grits', 'honey', 'oats', 'syrup', 'tea']
inventory['aisle_2'] = ['apple sauce','canned fruits', 'dried fruits', 'canned juices', 'canned meats', 'prunes', 'raisins', 'canned seafood', 'soup' ]
inventory['aisle_3'] = ['baking soda', 'cake mixes', 'extracts', 'flour', 'gelatins', 'ketchup', 'mayonnaise', 'meal', 'mustard', 'oils', 'olives', 'pickles','pie crust','salad dressing', 'salt', 'spices', 'sugar', 'vinegar', 'yeast' ]
inventory['aisle_4'] = ['international foods', 'macaroni', 'canned mushrooms', 'rice', 'meat sauces', 'dried spaghetti', 'tomato paste', 'canned vegetables']
...
inventory['rear_left'] = ['eggs', 'milk', 'yogurt', 'sour cream']
inventory['rear_right'] = ['gluten free']
inventory['produce'] = ['vegetables']
shopping_list = {key: [] for key in inventory.keys()}
#Construct a while loop that takes user input
while True:
item = input('enter a grocery item you need:')
if item.lower() == 'quit':
break
in_store = False
for aisle, items in inventory.items():
if item.lower() in items:
shopping_list[aisle].append(item)
in_store = True
break
if not in_store:
print('item not available')
推荐阅读
- python - 我想从第 2 行删除行到 CSV 文件中的特定数字,但它会从文件中删除所有
- saxon - 直接使用 Saxon API 使用系统函数
- php - 如何更改自定义 WordPress 管理页面的输入字段的值?
- python - 有没有办法用 python sqlite3 迭代地将列输入到数据库中?
- json - Firebase 数据库完整 JSON 列表 - Flutter
- python - Plotly 中的多级树形图
- zsh - 更改 zsh 历史颜色
- azure-virtual-machine - 2 个 Azure VM 之间的服务终结点?
- go - 为什么 Go 生成的 protobuf 文件包含互斥锁?
- android - 应用程序关闭时警报管理器不起作用