python打印列表中指定元素的所有下标(5种方法)
来源:互联网 发布:澳门网络彩票 编辑:程序博客网 时间:2024/06/06 17:39
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)
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:
0
1
6
9
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
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
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
(完)
1 0
- python打印列表中指定元素的所有下标(5种方法)
- java基础--输出数组中指定元素的下标
- 删除数组中指定元素不是下标
- Python 查找有序列表中指定元素所在位置
- list -- set方法——替换列表中指定位置的元素
- 删除链表中指定的所有元素
- window.print()打印网页中指定内容的实现方法
- window.print()打印网页中指定内容的实现方法
- window.print()打印网页中指定内容的实现方法
- 找出设备中指定的所有应用的方法
- php删除数组中指定值的元素的几种方法
- php删除数组中指定值的元素的几种方法
- 删除solr中指定core的所有数据(索引)
- JavaScript Array(数组)对象中指定元素的删除
- STL:map的使用:删除map中指定值为value的所有元素
- STL:map的使用:删除map中指定值为value的所有元素
- 删除vector中指定的元素
- java取集合中指定的元素
- java图片裁切工具类
- HDU 1004 Let the Balloon Rise
- Gem: whenever定时执行任务(rake脚本的编写)
- leetcode 344 Reverse String
- POJ 2417 Discrete Logging(BSGS算法,gcd(a,p)=1)
- python打印列表中指定元素的所有下标(5种方法)
- android的帧动画和补间动画总结
- CDH中impala读取不到hive创建的表
- ds1302写时间
- Codeforces Round#321 Kefa and Dishes(状压DP)
- Cow Contest poj3360()
- mysql性能优化-慢查询分析、优化索引和配置
- iOS 【如何把项目托管到GitHub】
- zencart 选择支付方式