python中的序列有哪些_python中的序列

在python中内置了多种序列, 比较重要的序列有三个, 分别是字符串、列表和元祖, 序列是一种数据结构

序列的通用操作

索引

# 字符串

str = 'hello'

print(str[0]) # h

print(str[-1]) # 0

# 列表

lst = [1, 'hi', True]

print(lst[0]) # 1

print(lst[-1]) # True

# 元祖

tup = (1, 'hi', True)

print(tup[0]) # 1

print(tup[-1]) # True

切片

str = 'hello'

print(str[1:2]) # e

print(str[:2]) # he

print(str[1:]) # ello

print(str[1:2:2]) # 步长为2, e

print(str[::2]) # hlo

加法

s1 = 'hello '

s2 = 'world'

print(s1 + s2) # hello world

lst1 = [1, 2, 3]

lst2 = [4, 5, 6]

print(lst1 + lst2) # [1, 2, 3, 4, 5, 6]

乘法

s1 = 'hello'

print(s1 * 5) # hellohellohellohellohello

lst1 = [1, 2]

print(lst1 * 3) # [1, 2, 1, 2, 1, 2]

元素是否存在

s1 = 'hello'

print('l' in s1) #True

lst1 = [1, 'hello', True]

print('he' in lst1); #False

长度、最大值、最小值

//长度

s1 = 'hello'

print(len(s1)) # 5

lst = [1, 2, 3, 'hello', True]

print(len(lst)) # 5

//最大值最小值

lst = [1, 2, 3, 6, 2]

print(max(lst)) #6

print(min(lst)) #1

列表

list

lst = list('hello')

print(lst) # ['h', 'e', 'l', 'l', 'o']

print(''.join(lst)) # hello

修改删除元素

# 修改元素的值

lst = [1, 2, 3, 4]

lst[2] = 6

print(lst) # [1, 2, 6, 4]

# 删除元素

del lst[1]

print(lst) # [1, 6, 4]

# 给切片赋值

lst[2:] = list('hello')

print(lst) # [1, 6, 'h', 'e', 'l', 'l', 'o']

列表方法

lst = [1, 2, 3]

# append, 追加到末尾

lst.append(4)

print(lst) # [1, 2, 3, 4]

# clear, 清空列表

lst.clear()

print(lst) # [] 等价于 lst[:] = []

# copy, 会拷贝一个副本

lst1 = [1, 2, 3]

lst2 = lst1

lst2[1] = 10

print(lst1) # [1, 10, 3]

lst3 = lst1.copy()

lst3[1] = 20

print(lst1) # [1, 10, 3]

# count, 指定元素在列表中出现的次数

lst4 = [1, 2, 3, 4, 4, 2, 4]

print(lst4.count(4)) # 3

# extend, 用一个列表扩展另外一个列表

a = [1, 2, 3]

b = [4, 5, 6]

a.extend(b)

print(a) # [1, 2, 3, 4, 5, 6], 和+的区别, +并不会修改a

# index, 指定值第一个出现的索引

print(a.index(1)) # 0

# insert, 将一个对象插入到列表

c = [1, 2, 3]

c.insert(2, 5)

print(c) # [1, 2, 5, 3]

# pop, 删除, 末尾删除, 并返回这一元素, 出栈, 没有提供push, 可以使用append代替

d = [1, 2, 3]

print(d.pop()) # 3

print(d) # [1, 2]

# remove, 删除第一个为指定值的元素

e = [1, 2, 4, 5]

e.remove(4)

print(e) # [1, 2, 5]

# reverse, 反转

f = [1, 2, 3]

f.reverse()

print(f) # [3, 2, 1]

# sort, 排序

g = [4, 6, 2, 7, 3]

g.sort()

print(g) # [2, 3, 4, 6, 7]

元祖

和列表一样, 唯一的不同就是元祖不能修改

t = (1, 2, 3)

// 列表转元祖

t1 = tupe([1, 2, 3])

字符串

字符串是不可变的,因此所有的元素赋值和切片赋值都是非法的

center

通过填充字符(默认空格)来让字符串居中

s1 = 'hello'

s2 = s1.center(30)

s3 = s1.center(30, '*')

print(s2) # hello

print(s3) # ************hello*************

find

在字符串中查找子串, 存在就返回索引, 不存在就返回-1

s1 = 'hello'

index1 = s1.find('l')

print(index1) # 2

s2 = 'hello world'

index2 = s2.find('world') # 6

index3 = s2.find('world', 1, 7) # -1, 第二个参数包含起点,第三个参数不包含终点

print(index2)

print(index3)

join

与splite相反, 合并序列的元素

lst = ['1', '2', '3', '4']

s1 = ''.join(lst) # 1234

s2 = '-'.join(lst) # 1-2-3-4

print(s1)

print(s2)

lower

返回字符串的小写

'Hello'.lower() # hello

replace

一个字符串替换另外一个字符串, 并返回新串

'hello world'.replace('world', 'python') # hello python

split

与join相反, 字符串拆分为序列

'hello world'.split(' ') #['hello', 'world']

strip

将字符串的开头和末尾空白删除

' hello, world '.strip() # 'hello, world'

translate

和replace都是替换字符串,

判断是否满足条件

存在很多以is开头的方法, 判断字符串是否具有特定的性质, 如: islower, isupper.....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值