需求
找出list中某一元素并返回所有匹配index值
问题
使用index()
只能返回一个下标
>>> cw=[0,1,2,1,1,0,1,0,0,1]
>>> cw.index(1)
1
解决
利用enumerate()
函数构建元组
>>> [i for i,x in enumerate(cw) if x == 1 ]
[1, 3, 4, 6, 9]
找出list中某一元素并返回所有匹配index值
使用index()
只能返回一个下标
>>> cw=[0,1,2,1,1,0,1,0,0,1]
>>> cw.index(1)
1
利用enumerate()
函数构建元组
>>> [i for i,x in enumerate(cw) if x == 1 ]
[1, 3, 4, 6, 9]
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。