Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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中搜索2D数组_Python_Arrays_Search_Arraylist_Multidimensional Array - Fatal编程技术网

如何在python中搜索2D数组

如何在python中搜索2D数组,python,arrays,search,arraylist,multidimensional-array,Python,Arrays,Search,Arraylist,Multidimensional Array,我是Python的初学者,我想在2D数组中搜索 stock_file = [[21345678, 2.30, "Banana"], [12345670, 3.50, "Apple"]] 我希望用户输入一个八位代码,然后输出“找到的产品”(如果它在数组中)。我尝试了以下代码: if eight_digit_code in stock_file: print("Product found") else: print("product not found") 但是,即使代码与数组中

我是Python的初学者,我想在2D数组中搜索

stock_file = [[21345678, 2.30, "Banana"], [12345670, 3.50, "Apple"]]
我希望用户输入一个八位代码,然后输出
“找到的产品”
(如果它在数组中)。我尝试了以下代码:

if eight_digit_code in stock_file:
    print("Product found")
else:
    print("product not found")
但是,即使代码与数组中的代码匹配,代码也不会输出
“找到的产品”
。我的代码有问题吗?
请注意,我并没有试图找到代码的索引。我想搜索代码是否在数组中。

您可以使用类似以下内容:

>>> stock_file = [[21345678,2.30,'Banana'],[12345670,3.50,'Apple']]
>>> code=int(input('Enter 8 digit code: '))
Enter 8 digit code: 12345670
>>> list(filter(lambda x:x[0]==code, stock_file))
[[12345670, 3.5, 'Apple']]
>>> code=int(input('Enter 8 digit code: '))
Enter 8 digit code: 21345678
>>> list(filter(lambda x:x[0]==code, stock_file))
[[21345678, 2.3, 'Banana']]
>>> 

我建议在发布问题之前,用谷歌搜索问题的标题作为最后的检查。目前的“二硅酸盐”目标回答了一个不同的问题。这里不太适用。让我们滥用电动工具:
[21345678,mock.ANY,mock.ANY]在库存文件中
表示
正确
;-)