您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页python序列(三)列表元素访问与计数

python序列(三)列表元素访问与计数

来源:爱go旅游网
python序列(三)列表元素访问与计数

  1.使⽤下标直接访问列表元素,如果指定下标不存在,则抛出异常。>>> alist[3]1

>>> alist[3]=5.5>>> alist

[1, 3, 5, 5.5, 3, 5, 1, 3, 5]>>>

>>> alist[15]

Traceback (most recent call last):

File \"\ alist[15]

IndexError: list index out of range

  2.使⽤列表对象的index()⽅法获取指定元素⾸次出现的下标,若列表对象中不存在指定元素,则抛出异常。>>> alist

[1, 3, 5, 5.5, 3, 5, 1, 3, 5]>>> alist.index(3)1

>>> alist.index(100)

Traceback (most recent call last):

File \"\ alist.index(100)

ValueError: 100 is not in list

  3.count()⽅法统计指定元素在列表对象中出现的次数>>> alist

[1, 3, 5, 5.5, 3, 5, 1, 3, 5]>>> alist.count(3)3

>>> alist.count(5)3

>>> alist.count(4)0

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务