'''
Created on 2018年3月16日
@author: PENGCHUNTAO524
'''
myList = ['Google', 'Runoob', 1997, 'Runoob']
myListlength = len(myList)
print('长度为:' + str(myListlength))
for x in myList:
idx = myList.index(x, )
idxStr = str(idx)
# print(idxStr)
print(idxStr ,end='')
print("," + str(x))
print("======================")
for y in range(myListlength):
print(str(y) + " , " + str(myList[y]))
上面代码的结果为:
长度为:4
0,Google
1,Runoob
2,1997
1,Runoob
======================
0 , Google
1 , Runoob
2 , 1997
3 , Runoob
上面的代码中的列表Runoob是重复的,如果使用idx = myList.index(x, )只能拿到第一次出现的该字符的索引,第2个方法就可以实现拿到所有的元素的索引