Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用用户输入修改列表_Python - Fatal编程技术网

在python中使用用户输入修改列表

在python中使用用户输入修改列表,python,Python,我想知道我怎样才能接受这个用户输入(如下)并修改列表中已经存在的项目。如果用户没有输入与列表中显示的相同的项目,则只需将输入添加为新的杂货项目 列表 inventory = [['Milk, 3.99, 25'], ['Bread, 1.99, 35'], ['Eggs, 1.99, 50'], ['Flour, 0.52, 20'], ['Rice, 0.72, 3

我想知道我怎样才能接受这个用户输入(如下)并修改列表中已经存在的项目。如果用户没有输入与列表中显示的相同的项目,则只需将输入添加为新的杂货项目

列表

inventory = [['Milk,   3.99,  25'],
             ['Bread,  1.99,  35'],
             ['Eggs,   1.99,  50'],
             ['Flour,  0.52,  20'],
             ['Rice,   0.72,  35']]
modItem = input("Enter the name of an existing item you want to modify:")
modPrice = input("Enter the new price of the item:")
modStock = input("Enter the new stock of the item:")
inventory.append([modItem, modPrice, modStock])
输入

inventory = [['Milk,   3.99,  25'],
             ['Bread,  1.99,  35'],
             ['Eggs,   1.99,  50'],
             ['Flour,  0.52,  20'],
             ['Rice,   0.72,  35']]
modItem = input("Enter the name of an existing item you want to modify:")
modPrice = input("Enter the new price of the item:")
modStock = input("Enter the new stock of the item:")
inventory.append([modItem, modPrice, modStock])

尝试使用以下简单函数检查该项是否已存在:

def检查库存(修改项,库存):
尝试:
对于库存中的项目:
如果modItem==项[0]。拆分(',')[0]:
返回真值
除:
通过
返回错误
...
#然后,您可以调用您的函数
如果(检查库存(修改项,库存)):
inventory.append([modItem,modPrice,modStock])
第二种方法是将列表转换为dict。我建议您这样做。

您可以使用dict

inventory = {'Milk': [3.99,  25],
             'Bread':  [1.99,  35],
             'Eggs':  [1.99,  50],
             'Flour': [0.52,  20],
             'Rice': [0.72,  35]}

modItem = input("Enter the name of an existing item you want to modify:")
modPrice = float(input("Enter the new price of the item:"))
modStock = int(input("Enter the new stock of the item:"))

Enter the name of an existing item you want to modify:Milk
Enter the new price of the item:10
Enter the new stock of the item:12
如果项已经存在,则将替换它,否则将添加一个新的键值对

inventory[modItem] =  [modPrice, modStock]

inventory

{'Milk': [10.0, 12],
 'Bread': [1.99, 35],
 'Eggs': [1.99, 50],
 'Flour': [0.52, 20],
 'Rice': [0.72, 35]}


您的
库存
列表格式不正确。鉴于您现有的代码和数据的更正版本,以下代码以最直接的方式实现了您的目标:

inventory = [['Milk', '3.99',  '25'],
             ['Bread',  '1.99',  '35'],
             ['Eggs',   '1.99',  '50'],
             ['Flour',  '0.52',  '20'],
             ['Rice',   '0.72',  '35']]

modItem = input("Enter the name of an existing item you want to modify:")
modPrice = input("Enter the new price of the item:")
modStock = input("Enter the new stock of the item:")

for item in inventory:
    if item[0] == modItem:
        item[1] = modPrice
        item[2] = modStock
        break
else:
    inventory.append([modItem, modPrice, modStock])

此代码在现有库存上循环,查找用户输入名称的项目。如果找到名称,则包含该项目的项目将更新为新的价格和数量。如果未找到现有项,则会创建并添加一个新项。

您可以使用
枚举
并在迭代元素时修改列表:

inventory = [['Milk,   3.99,  25'],
             ['Bread,  1.99,  35'],
             ['Eggs,   1.99,  50'],
             ['Flour,  0.52,  20'],
             ['Rice,   0.72,  35']]

modItem = 'Milk' 
modPrice = 4
modStock = 30

found = False
for idx, elt in enumerate(inventory):
    if modItem in elt[0]:
        inventory[idx] = [f"{modItem}, {modPrice}, {modStock}"] # modify the element at the found index
        found = True
        break

if not found:
        inventory.append([f"{modItem}, {modPrice}, {modStock}"])
print(inventory)
输出:

[['Milk, 4, 30'], ['Bread,  1.99,  35'], ['Eggs,   1.99,  50'], ['Flour,  0.52,  20'], ['Rice,   0.72,  35']]
[['Milk', 4, 30], ['Bread', 1.99, 35], ['Eggs', 1.99, 50], ['Flour', 0.52, 20], ['Rice', 0.72, 35]]
此外,我建议保留内部列表作为元素列表。目前,它只是一个带有
str
对象的单个元素

inventory = [['Milk', 3.99, 25],
             ['Bread', 1.99, 35],
             ['Eggs', 1.99, 50],
             ['Flour', 0.52, 20],
             ['Rice', 0.72, 35]]

found = False
for idx, elt in enumerate(inventory):
    if modItem == elt[0]:
        inventory[idx] = [modItem, modPrice, modStock]
        found = True
        break

if not found:
    inventory.append([modItem, modPrice, modStock])
print(inventory)
输出:

[['Milk, 4, 30'], ['Bread,  1.99,  35'], ['Eggs,   1.99,  50'], ['Flour,  0.52,  20'], ['Rice,   0.72,  35']]
[['Milk', 4, 30], ['Bread', 1.99, 35], ['Eggs', 1.99, 50], ['Flour', 0.52, 20], ['Rice', 0.72, 35]]

欢迎来到堆栈溢出。你试过什么?你知道如何迭代列表吗?一步一步地解决这个问题。到目前为止,它接受用户的输入并将其放入列表中,但我想知道如果用户输入与列表中所示相同的商品名称,程序如何修改价格和库存。@Rajesh-hellies。我想知道我对你问题的回答是否有用。它回答了你的问题吗?如果是这样,我鼓励您通过选中标记按钮投票和/或接受它作为正确答案。请注意,接受答案会得到分数。