如何删除、更新和搜索两个独立的不同列表



我有一个程序,它有两个列表;一个代表名称(库存(,一个代表数量。如何根据名称(库存(列表删除、更新和搜索,并对数量列表执行适当的操作。例如,如果我有一个五个项目的列表,而这五个数量在不同的列表中按道具顺序排列。如果我删除了第三项,我该如何删除该项目的数量?以下是完整的代码:

import os

name = []
qty = []
class Inventory:
def __init__(self,desc,unit):
self.description = desc
self.units = unit
class Totals:
def __init__(self):
self.inventory=[]
def adding(self,inventory):
#adding item to list
self.inventory.append(inventory)
def show_items(self):
if not self.inventory:
return
print("{:<15}{:<25}".format(" "*9,"Description","Units in inventory"))
print("-"*75)
for c,i in enumerate(self.inventory,1):
print("{:<15}{:<25}{:<25}{:<10}".format("Item #"+str(c),i.description,i.units))
print("-"*75)

def menuDisplay():
print ('=============================')
print ('= Inventory Management Menu =')
print ('=============================')
print ('(1) Add New Item to Inventory')
print ('(2) Remove Item from Inventory')
print ('(3) Update Inventory')
print ('(4) Search Item in Inventory')
print ('(5) Print Inventory Report')
print ('(99) Quit')
CHOICE = int(input("Enter choice: "))
menuSelection(CHOICE)
def menuSelection(CHOICE):
if CHOICE == 1:
print('Adding Inventory')
print('================')
new_name = input('Enter the name of the item: ')
name.append(new_name)
new_qty = int(input("Enter the quantity of the item: "))
qty.append(new_qty)
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
elif CHOICE == 99:
exit()
elif CHOICE == 2:
print('Removing Inventory')
print('==================')
removing = input('Enter the item name to remove from inventory: ')
name.remove(removing)
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
elif CHOICE == 99:
exit()
elif CHOICE == 3:
print('Updating Inventory')
print('==================')
input('Enter the item to update: ')
update = int(input("Enter the updated quantity. Enter 5 for additional or -5 for less: "))
if update == 5:
print()
elif update == -5:
print()
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
elif CHOICE == 99:
exit()
updateInventory()
elif CHOICE == 4:
print('Searching Inventory')
print('===================')
search = input('Enter the name of the item: ')
if search in search:
print ('Item:     ', item_description)
print ('Quantity: ', item_quantity)
print ('----------')
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
elif CHOICE == 99:
exit()
searchInventory()
elif CHOICE == 5:
print('Current Inventory')
print('=================')
input('Enter the name of the item: ')
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
elif CHOICE == 99:
exit()
printInventory()
elif CHOICE == 99:
exit()

def printInventory():
InventoryFile = open('Inventory.txt', 'r')
item_description = InventoryFile.readline()
print ('Current Inventory')
print ('-----------------')
while item_description != '':
item_quantity = InventoryFile.readline()
item_description = item_description.rstrip('n')
item_quantity = item_quantity.rstrip('n')
print ('Item:     ', item_description)
print ('Quantity: ', item_quantity)
print ('----------')
item_description = InventoryFile.readline()
InventoryFile.close()
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
if CHOICE == 98:
menuDisplay()
else:
exit()

menuDisplay()

我相信添加选项有效,删除选项也有效,但我不知道如何删除数量和项目名称。

以下是我要做的。不过,您需要了解两个新函数。

第一个新功能

.remove将删除与要删除的内容相匹配的项目。然而,就你的情况而言,我认为你正在寻找.pop()pop

.pop()接受一个数字,该数字是要删除的项目的索引。

第二个函数

现在您必须知道.index,它返回您输入到函数中的字符串的索引。索引

组合

既然你知道了这2个功能,那就很简单了!使用第一个列表中的.index(),然后在第二个列表中使用.pop

lis = ["hello", "bye", "sigh"]
lis2 = [10, 20, 30]
takeout = input("What do you want to delete?")
indexdel = lis.index(takeout)
lis.pop(indexdel)
lis2.pop(indexdel)

这将删除他们两个!现在,应用于您自己的代码。

出于您的目的,它将是:

elif CHOICE == 2:
print('Removing Inventory')
print('==================')
removing = input('Enter the item name to remove from inventory: ')
indexdel = name.index(removing)
name.pop(indexdel)
qty.pop(indexdel)
CHOICE = int(input('Enter 98 to continue or 99 to exit: '))

还有!处理关于单个对象的评论

这也是我的第一步,但我觉得一开始有点困惑。不过,我也可以证明这一点!

让我们创建一个类来存储这两个值。

class Foo():
def __init__(self, name, qty):
self.name = name
self.qty = qty

现在,您可以在一个列表中创建几个对象,如:

item = []
item.append(Foo("hello", 50))
item.append(Foo("bye", 100))
def delitem(name):
for i in item:
if i.name == name:
item.remove(i)

最新更新