Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 在any()函数中,有没有方法返回返回返回True的元素的索引?_Python_Python 3.x_List_Any - Fatal编程技术网

Python 在any()函数中,有没有方法返回返回返回True的元素的索引?

Python 在any()函数中,有没有方法返回返回返回True的元素的索引?,python,python-3.x,list,any,Python,Python 3.x,List,Any,(这只是我想解决的问题的一个例子) 有没有一种方法可以返回返回True的元素的索引,或者我应该只使用for循环?事实上,您需要数组中语句的索引 array = ["hello", "hi"] statement = input("Give a word: ") condition = array.index(statement) if statement in array else -1 print(condition) 演示 下次搜

(这只是我想解决的问题的一个例子)


有没有一种方法可以返回返回True的元素的索引,或者我应该只使用for循环?

事实上,您需要
数组中语句的
索引

array = ["hello", "hi"]
statement = input("Give a word: ")
condition = array.index(statement) if statement in array else -1
print(condition)
演示


下次搜索函数属性。我编写的示例代码基于

array=[“hello”、“hi”、“example”、“to get”、“index from array”]
def SearchIndex():
语句=输入()
#//检查输入是否为列表中的项目
数组中的if语句:print(“>>”,array.index(语句))
#//如果输入是列表中项目的一部分,则进行其他搜索
其他:
第i部分=错误
对于数组中的项:
项目中的if语句:
print('>>{0}是“{2}”->{1}”格式的一部分(语句、array.index(item)、array[array.index(item)])
#//如果发现信号
第_部分=真
#//如果未找到(信号为假)
如果第_部分==False:print(f“>>{statement}不在列表中…”)
而1:
SearchIndex()

什么是
elm
、集合、列表?什么是查询?未使用语句?您所说的“返回true的元素”是什么意思?你打算用这个索引做什么?我知道如何使用for循环获取索引,我问是否有不使用for循环获取索引的方法,谢谢@LittleNooblet
数组。索引(语句)
不是循环。我在if语句之后调用它以避免错误。后面是什么:只是一个例子,说明了如果只定义了一个单词,如何获取索引
array.index(statement)
将返回int(),前提是statement和数组项之间完全匹配
array = ["hello", "hi"]
statement = input("Give a word: ")
condition = array.index(statement) if statement in array else -1
print(condition)
Give a word: hello
0
Give a word: hi
1
Give a word: Hi
-1