简体   繁体   English

Python-根据输入从数组中减去值

[英]Python - Subtracting a value from an array based on input

product = [['p3','p5','p7'], ['16GB','32GB'],['1TB','2TB'], ['19in','23in'], ['Mini Tower', 'Midi Tower'], ['2 ports','4 ports']]
stock = [5,5,5,5,5,5,5,5,5,5,5,5,5]
chosen = []
for part in product:
  for i in part:
    if input('Would you like the following component: '+i) == 'y':
      print("Selected: ", i)
      chosen.append(i)
      position = product[part].index(chosen)
      stock -= stock[position]
      print(stock)
      break

I'm having a problem defining 'position', the aim of the variable is to find the position of the input (eg. p3) and then apply that to subtracting the corresponding stock value. 我在定义“位置”时遇到问题,变量的目的是找到输入的位置(例如p3),然后将其应用于减去相应的库存值。 Does anyone have any ideas how I can fix this? 有谁知道如何解决这个问题? Much appreciated. 非常感激。

Basically it is like 2D array: 基本上就像二维数组:

try like this. 尝试这样。

In Python we can write like for any 2D array. 在Python中,我们可以像编写任何2D数组那样编写。

for i in range(len(product)):
    for j in range(len(product[i])):
        print(product[i][j],i,j)

Note: Your code has lot of errors , I am just stating how to access and get the location of an array variable in python 注意:您的代码有很多错误,我只是在说明如何在python中访问和获取数组变量的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM