首页 > 其他 > 详细

列表和字符串内置方法

时间:2019-09-15 14:55:35      阅读:105      评论:0      收藏:0      [点我收藏+]

序列类型(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 ...

列表和字符串内置方法

原文:https://www.cnblogs.com/YajunRan/p/11521941.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!