如何遍历列表并搜索多个列表
How to iterate through list and search for several lists
这些是杂货店清单:
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)
出于某种原因,整个事情都被忽略了,当我打印一个应该更改的列表时,它只是显示为空。例如,用户输入 honey 和 grits,在 For 循环中它应该遍历列表并看到 honey 在 aisle one 中并将其添加到 a1 然后看到 grits 也在 aisle one 中并将其添加到 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,而不是打印应该在过道 1 中的杂货,它只会打印空白。前任。 []
确保您使用的是 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')
这些是杂货店清单:
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)
出于某种原因,整个事情都被忽略了,当我打印一个应该更改的列表时,它只是显示为空。例如,用户输入 honey 和 grits,在 For 循环中它应该遍历列表并看到 honey 在 aisle one 中并将其添加到 a1 然后看到 grits 也在 aisle one 中并将其添加到 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,而不是打印应该在过道 1 中的杂货,它只会打印空白。前任。 []
确保您使用的是 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')