列表和字符串内置方法

序列类型(Sequence Types)

序列类型包括:list, tuple, range, 补充:字符串类型,二进制数据类型(binary data)
其中包括可变序列类型:如list,不可变序列类型: 如字符串类型,tuple类型

可变序列类型操作

1. 列表

序列类型共有操作:索引取值,索引切片,for循环取值,成员运算,index查找索引位置, len 长度, “+”运算

增加元素:append, expand, insert

删除元素:pop, del, remove, clear

排序相关:sort, reverse

lst = [0 for i in range(10)]
lst
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
lst[:5] = range(1,6)
lst
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
lst *= 2
lst
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
lst.pop(0)
lst
[2, 3, 4, 5, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
[func for func in dir(lst) if not func.startswith("__")]
['append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']

不可变序列类型操作

字符串

[func for func in dir("str") if not func.startswith("__")]
['capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isascii',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

序列类型共有操作:索引取值,索引切片,for循环取值,成员运算,index查找索引位置, len 长度, “+”运算

缩短字符:strip, lstrip, rstrip

增加字符:center, ljust, rjust, zfill

切分和连接:split (rsplit), join, splitlines

转换或替代:upper, lower, swapcase, title, capitalize, replace, translate

判断:isdigit, isalpha, startswith, endswith ...

猜你喜欢

转载自www.cnblogs.com/YajunRan/p/11521941.html
今日推荐