我如何找到,如果一个int在数组的数组? [英] How do I find if an int is in array of arrays?

查看:133
本文介绍了我如何找到,如果一个int在数组的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有每个索引是另一个数组的数组。如果我有一个int,我怎么能写code,检查是否INT是第2 indicies在Python的阵列中的每个数组元素内present。

I have an array with each index being another array. If I have an int, how can I write code to check whether the int is present within the first 2 indicies of each array element within the array in python.

例如:
3

array = [[1,2,3], [4,5,6]] 

会产生错误。

3

array = [[1,3,7], [4,5,6]] 

会产生真。

推荐答案

可以的的阵列来获得它的一部分,然后在 $ C>运营商和的()函数是这样的:

You can slice your array to get a part of it, and then use in operator and any() function like this:

>>> array = [[1,2,3], [4,5,6]]
>>> [3 in elem[:2] for elem in array]
[False, False]
>>> any(3 in elem[:2] for elem in array)
False

>>> array = [[1,3,7], [4,5,6]]
>>> [3 in elem[:2] for elem in array]
[True, False]
>>> any(3 in elem[:2] for elem in array)
True

的()函数返回如果可迭代的元素至少有一个是

any() function returns True if at least one of the elements in the iterable is True.

这篇关于我如何找到,如果一个int在数组的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆