前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python打印列表中指定元素的所有下标

python打印列表中指定元素的所有下标

作者头像
py3study
发布2020-01-03 12:33:34
2.9K0
发布2020-01-03 12:33:34
举报
文章被收录于专栏:python3

1》法一: song@ubuntu:~$ vi find2.py song@ubuntu:~$ more find2.py l=[1,2,3,4,7,2,5,6,2,8,9,0] first=0 for i in range(l.count(2)):     new_l=l[first:]     index=first+new_l.index(2)     print 'find the index of 2:',index     first=index+1 song@ubuntu:~$ python find2.py find the index of 2: 1 find the index of 2: 5 find the index of 2: 8 song@ubuntu:~$  2》法二: song@ubuntu:~$ vi find_2.py song@ubuntu:~$ more find_2.py l=[2,2,3,4,5,1,2,3,1,2,3,4,5] first=True for i in range(l.count(2)):     if first==True:         pos=l.index(2)         first=False     else:         pos=l.index(2,pos+1)

    print pos

song@ubuntu:~$ python find_2.py 0 1 6 9 song@ubuntu:~$ 3》法三: song@ubuntu:~$ vi find_2_1.py song@ubuntu:~$ more find_2_1.py l=[2,2,3,4,5,1,2,3,1,2,3,4,5] for i in range(len(l)):     if l[i]==2:

        print i

song@ubuntu:~$ python find_2_1.py 0 1 6 9

song@ubuntu:~$ 

4》法四:

song@ubuntu:~$ vi find_2.py song@ubuntu:~$ more find_2.py l=[2,2,3,4,5,1,2,3,1,2,3,4,5] for i in range(l.count(2)):     if i==0:         pos=l.index(2)     else:         pos=l.index(2,pos+1)     print pos song@ubuntu:~$ python find_2.py 0 1 6 9

5》法五:

song@ubuntu:~$ vi find_2.py song@ubuntu:~$ more find_2.py l=[2,2,3,4,5,1,2,3,1,2,3,4,5] pos=-1 for i in range(l.count(2)):     pos=l.index(2,pos+1)     print pos song@ubuntu:~$ python find_2.py 0 1 6 9

(完)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档