【发布时间】:2015-07-01 22:38:49
【问题描述】:
我正在编写一个从文本文件中读取数据的代码。我使用 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']
现在我想在 Shop_Products 数组中找到这些“项目”索引,而不进行 for 循环和 if 检查。
我想知道是否可以使用任何 numpy 方法来完成:我想使用 intercept1d 来查找常见项目,然后使用 searchsorted。但是,我不能对我的“产品”列表进行排序,因为我不想松开原始排序(例如,我会使用索引直接查找每个产品的库存)。
关于“pythonish”解决方案的任何建议?
【问题讨论】:
标签: python arrays string numpy indexing