“Python Znajdź na liście” Kod odpowiedzi

Python Znajdź element na liście

>>> ["foo", "bar", "baz"].index("bar")
1
Cook's Tree Boa

Python Znajdź na liście

# There is several possible ways if "finding" things in lists.
'Checking if something is inside'
3 in [1, 2, 3] # => True
'Filtering a collection'
matches = [x for x in lst if fulfills_some_condition(x)]
matches = filter(fulfills_some_condition, lst)
matches = (x for x in lst if x > 6)
'Finding the first occurrence'
next(x for x in lst if ...)
next((x for x in lst if ...), [default value])
'Finding the location of an item'
[1,2,3].index(2) # => 1
[1,2,3,2].index(2) # => 1
[1,2,3].index(4) # => ValueError
[i for i,x in enumerate([1,2,3,2]) if x==2] # => [1, 3]
Bored Coder

Jak wyszukać element na liście w Python

l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
index_of_4 = l.index(4)
print(index_of_4)
##output:
## 3
IC

Znajdź listę Python

3 in [1, 2, 3] # => True
Poor Partridge

Python Znajdź, czy część listy znajduje się na liście

'''    
    check if list1 contains all elements in list2
'''
result =  all(elem in list1  for elem in list2)
if result:
    print("Yes, list1 contains all elements in list2")    
else :
    print("No, list1 does not contains all elements in list2"
Fair Finch

Odpowiedzi podobne do “Python Znajdź na liście”

Pytania podobne do “Python Znajdź na liście”

Więcej pokrewnych odpowiedzi na “Python Znajdź na liście” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu