序列通用操作
判断值是否属于序列
lst= [ 1 , 2 , 3 , 4 ]
a, b= 1 , 2
a in lst : True
b not in lst: False
序列连接,运算
lis1+ lis2
lis* 3
下标索引
lis[ 0 ]
lis[ - 1 ]
切片
lis[ 1 : 2 ] : [ lis[ 1 ] ] 不等于lis[ 1 ]
lis[ : - 1 ] : 除了最后一个数
步长
lis[ : : 2 ]
序列内置全局函数
len ( )
max ( )
min ( )
sum ( )
lis. index( a) : 返回lis序列中首个值为a的下标
lis. count( a) : 返回值为a出现的次数
列表list操作
列表与生成器
x1= range ( 5 )
type ( x1) : class 'range'
list ( x1)
list添加元素
lis. append( 'insert' )
lis. extend( [ a, b, c] )
list删除元素
lis. remove( )
lis. clear( )
list插入元素
lis. insert( num, 'insert' )
复制
lis. copy( )
排序
lis. sort( )
sorted ( lis)
与元组的差别
tup= 1 , 2 , 3
tup= ( 1 , )
tup[ 1 ]
删除所有的值为m
a= [ 1 , 2 , 3 , 3 , 4 ]
def removeallm ( a, m) :
lis= [ ]
for i in a:
if m!= i:
lis. append( i)
return lis
removeallm( a, 3 )
文本str操作
通用功能
str1[ : : 2 ]
len ( )
str1. index( )
常用功能
st. replace( 'old' , 'new' , num)
st. split( ',' )
m= '-'
lis= [ "a" , "b" , "c" ]
st= m. join( lis) : 'a-b-c'
st. startswith( ' ' )
st. endswith( ' ' )
st. upper( )
st. lower( )
st. swapcase( )
st. capitalize( )
st. isnumeric( )
st. isdigit( )
st. isalpha( )
st. rstrip( )
格式化字符
% i
% s
% f
% . 0f
% e
% g