Python 官方文档:入门教程 => 点击学习
1》法一: song@ubuntu:~$ vi find2.py song@ubuntu:~$ more find2.py l=[1,2,3,4,7
print pos
print i
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
(完)
--结束END--
本文标题: python打印列表中指定元素的所有下标
本文链接: https://www.lsjlt.com/news/192141.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0