如何在python中获取索引

代码示例

17
0

list中item的python索引

list.index(element)
5
0

list中元素的python索引

# alphabets list
alphabets = ['a', 'e', 'i', 'o', 'g', 'l', 'i', 'u']

# index of 'i' in alphabets
index = alphabets.index('i')   # 2
print('The index of i:', index)

# 'i' after the 4th index is searched
index = alphabets.index('i', 4)   # 6
print('The index of i:', index)

# 'i' between 3rd and 5th index is searched
index = alphabets.index('i', 3, 5)   # Error!
print('The index of i:', index)
4
0

如何在python中获取索引

list.index(element, start, end)
1
0

如何索引python

#you can index in a string like:
string='cupcake'
string[0]#returns 'c'
#you can index in a list like:
list = ['red', 'blue', 'green']
list[0]#returns 'red'
list[0][0]#returns[0] of 'red' wich is r

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................