我正在编写一段代码,从文本文件中读取数据。我使用numpy loadtxt 加载数据,可能会像这样:
import numpy as np
Shop_Products = np.array(['Tomatos', 'Bread' , 'Tuna', 'Milk', 'Cheese'])
Shop_Inventory = np.array([12, 6, 10, 7, 8])
我想检查一些我拥有的产品:
Shop_Query = np.array(['Cheese', 'Bread']
现在我想在不使用for循环和if检查的情况下,在Shop_Products数组中找到这些“items”的索引。我想知道是否可以使用numpy方法之一来完成:我考虑使用intercept1d来查找公共项,然后使用searchsorted。但是,我不能对我的“Products”列表进行排序,因为我不想失去原始排序(例如,我将使用索引直接查找每个产品的库存)。有关“pythonish”解决方案的任何建议?
in1d
更好 - 根据查询的顺序返回索引的能力特别有用。 - Alex Riley