python取出列表的第一列_Python取出列表相应值的位置(表处理)

#需求

在一个列表中,取出相应值的位置

方法1:

#脚本示例[root@localhost opt]# cat list.py

#!/usr/bin/env python

#_*_ coding:utf-8 _*_

name=['!','#','*','Eric','wsyht','jack','jack','a','b','c','d',1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,2332,4,2,6,2]

first_pos = 0

for i in range(name.count(2)): #5个2

new_list = name[first_pos:]

next_pos = new_list.index(2) + 1 #第一个元素位置在12 , 再+1,下个一位置开始寻找下一个元素位置

print 'Find postion:', first_pos + new_list.index(2),'Next:', next_pos #12, 13

first_pos += next_pos #0+13

#执行脚本[root@localhost opt]# python list.py

Find postion: 12 Next: 13

Find postion: 18 Next: 6

Find postion: 24 Next: 6

Find postion: 31 Next: 7

Find postion: 33 Next: 2

方法2:

#脚本示例#!/usr/bin/env python

#conding:utf-8

a = [1,0,2,3,4,5,6,7,8,1,2,3,4,5,6,1,2,3,4,5]

pos = 0

for i in range(a.count(2)): #统计出现2的次数

if pos == 0:

pos = a.index(2) #查找第一个值出现的位置,赋值为pos

else:

pos = a.index(2,pos+1) #2为查找的数,从pos+1的位置开始找

print pos

#执行脚本[root@localhost opt]# python list_count.py

12

18

24

31

33

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值