有没有一种方法可以返回在any()函数中返回True的元素的索引?

时间:2020-07-11 08:33:27

标签: python python-3.x list any

(这只是我要解决的问题的一个示例)

array = ["hello", "hi"]

statement = input()

condition = any(statement in elm for elm in array)

是否可以返回返回True的元素的索引,还是应该只使用for循环?

2 个答案:

答案 0 :(得分:0)

实际上,您希望el.find( "path" ).css( "pointer-events": "none" );中的语句的index

array

演示

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

答案 1 :(得分:0)

下次搜索功能属性。我在Python documents

上编写了示例代码
array = ["hello", "hi", "example", "to get", "index from array"]

def SearchIndex():
    statement = input()
    #// Check if input is item on list
    if statement in array: print(">>", array.index(statement))

    #// Other search if input is part of item in list
    else:
        part_of = False
        for item in array:
            if statement in item:
                print('>> {0} is part of "{2}" -> {1}'.format(statement, array.index(item), array[array.index(item)]))
                #// If founded signal
                part_of = True
        #// If nout found (signal is false)
        if part_of == False: print(f">> {statement} is not on list...")

while 1:
    SearchIndex()