Python基础课:列表方法count(), index()

 1 >>> x = [1,2,2,2,2,2,3,3,3,3,4,4]
 2 >>> x.count(4) #元素4在列表x中出现的次数
 3 2
 4 >>> x.count(2)
 5 5
 6 >>> x.index(3) #元素3在列表x中首次出现的索引
 7 6
 8 >>> x.index(4)
 9 10
10 >>> x.index(5) #列表x中没有5,抛出异常
11 Traceback (most recent call last):
12   File "<pyshell#30>", line 1, in <module>
13     x.index(5) #列表x中没有5,抛出异常
14 ValueError: 5 is not in list
15 >>> 5 in x
16 False
17 >>> 3 in x
18 True
19 >>> 

 

posted on 2017-07-01 02:36  yue_bei  阅读(284)  评论(0编辑  收藏  举报

导航