获取python list的元素索引

2018-09-23  本文已影响0人  HelloWorld__
1 python list 元素无重复,如下:
list_test = ["a", "b", "c", "d"]
a_index = list_test.index("a")
c_index = list_test.index("c")
print a_index
print c_index

# 结果:
  0
  2
1 python list 元素有重复,如下:
list_test = ["a", "b", "a", "c", "c", "d"]
for index, value in enumerate(list_test):
        print index, value

# 结果:
  0 a
  1 b
  2 a
  3 c
  4 c
  5 d
上一篇 下一篇

猜你喜欢

热点阅读