首页 > 编程语言 > 详细

python查找列表中某个元素所有下标的两个方法

时间:2020-08-23 23:01:27      阅读:1439      评论:0      收藏:0      [点我收藏+]

方法一

技术分享图片

摘自Python查找列表中某个元素返回所有下标

 

方法二

name = [‘hello‘, ‘world‘, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3, ‘hello‘, ‘world‘, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3]
first_pos = 0
for i in range(name.count(‘a‘)):
    new_list = name[first_pos:]
    next_pos = new_list.index(2) + 1
    print(‘find ‘, first_pos + new_list.index(‘a‘))
    first_pos += next_pos

# 运行结果:
#   find  2
#   find  10

摘自:Python 查找list中的某个元素的所有的下标方法

 

python查找列表中某个元素所有下标的两个方法

原文:https://www.cnblogs.com/picassooo/p/13550975.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!