如何在python列表中查找某个元素的索引

2019-03-15
百度上回复别人的问题,几种方式的回答:
1)
print('*'*15,'想找出里面有重复数据的索引值','*'*15)
listA = [100, 94, 88, 82, 76, 70, 64, 58, 52, 46, 40, 34,76]
print('列表中第1次出现的位置 = ',listA.index(76))
2)
a_list = ['a','b','c','c','d','c']
find = 'c'
print('重复元素出现的位置索引分别是 = ',[i for i,v in enumerate(a_list) if v==find])

### print('重复元素出现的位置索引分别是 = ',[index for (index,value) in enumerate(a_list) if value==find])
       -----------------------------------------------------
 
        注解,语法(Copy from this help of Python): enumerate(iterable, start=0)

      Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. 
      enumerate() returns a tuple           ###这个值包含 (计数 值) 
               containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
        >>> list(enumerate(seasons))
        [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

>>> list(enumerate(seasons, start=1))
        [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]




      -----------------------------------------------------
3)### 求某个元素重复的索引值,函数方式表述如下:
a_list = ['a','b','c','c','d','c']
def unique_index(L,f):
"""L表示列表, i表示索引值,v表示values,f表示要查找的元素 """
return [i for (i,v) in enumerate(L) if v==f]
print('索引值 = ',unique_index(a_list,'c')) 运行结果:
索引值 = [2, 3, 5] 4)还缺一种,列出多个元素重复的索引值,以后再补充
......

最新文章

  1. Mysql 学习之基础操作
  2. 分享一个css3写的气泡对话框,适合于即时通讯和留言本的动态内容
  3. hbuilder中如何使用egit上传项目
  4. Dapper学习 - Dapper.Rainbow(一) - Create
  5. 我的Android第二章:Android目录结构
  6. Mysql之复制服务
  7. 传智168期JavaEE就业班 day03-js
  8. 把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方。Duplicate entry
  9. HTML常见标签总结
  10. 配置Git Extension免密码发布代码到CSDN
  11. 0到N数其中三个数的全排列
  12. MVVM模式应用 之为ApplicationBarIconButton 添加Command操作属性
  13. js 技巧1
  14. <iOS>UIImage变为NSData并进行压缩
  15. json和jquery中的ajax
  16. pyadb关于python操作adb的资料
  17. Call to localhost/127.0.0.1:9000 failed on connection exception:java.net.ConnectException的解决方案
  18. 【洛谷P1896】互不侵犯
  19. JS事件(四)坐标位置
  20. Neural Networks and Deep Learning 课程笔记(第二周)神经网络的编程基础 (Basics of Neural Network programming)

热门文章

  1. JavaScript事件坐标区别(offset,client,page)
  2. 201771010128王玉兰《面向对象程序设计(Java)》第十三周学习总结
  3. poj2391 最大流+拆点+二分答案+Floyd
  4. ubuntu 安装 swftoos
  5. 使用webstorm 搭建 vue 开发环境
  6. Android_四大组件之Service
  7. Java学习之路【第一篇】:前言
  8. 机器学习pdf资源
  9. Beta冲刺 —— 5.31
  10. 编程-Byte order & Bit order