“Daftar Python Temukan” Kode Jawaban

Python Temukan Indeks berdasarkan Nilai

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

Python temukan dalam daftar

# 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

Daftar Python Temukan

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

Jawaban yang mirip dengan “Daftar Python Temukan”

Pertanyaan yang mirip dengan “Daftar Python Temukan”

Lebih banyak jawaban terkait untuk “Daftar Python Temukan” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya