python之序列通用操作 作者:马育民 • 2018-07-11 00:53 • 阅读:10208 有序列如下: ```python l=[1,2,3,4] ``` ### 访问元素 通过索引访问元素 ```python >l[0] ``` 索引为负数,表示倒数第几个 ```python >l[-1] ``` 得到倒数第1个元素 ###截取元素 ```python L[1:] ``` 输出从第二个元素开始后的所有元素 ```python L[1:3] ``` 结果:[2, 3] **注意:** >**右面索引不包括** ### 判断元素是否存在于序列中 ```python >3 in [1, 2, 3] ``` ### not in 取反 ```python >3 not in [1, 2, 3] ``` ###返回某元素第一次出现的位置(从左往右) ```python l.index(3) ``` ###返回某元素第一次出现的位置(从右往左) ```python l.rindex(3) ``` ###获取序列长度 ```python len(l) ``` ###获取最大值 ```python max(l) ``` ###获取最小值 ```python min(l) ``` ###求和 ```python sum(l) ``` ### 连接序列 ```python 序列1 + 序列2 ``` ###统计元素出现的次数 ```python >l.count(2) ``` ### 重复序列元素 ```python [3]*4 ``` 列表中的元素3,重复4次 原文出处:http://www.malaoshi.top/show_1EF1UDDK9WKa.html