美文网首页
python中的序列

python中的序列

作者: iDevOps | 来源:发表于2019-08-26 21:49 被阅读0次

在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.....

相关文章

  • Python高级知识点学习(四)

    序列类型 Python中的序列类型,序列类型可以使用for循环遍历。 序列类,序列是python中非常重要的协议,...

  • Python 序列类型 (1) - 序列类型分类

    Python中的序列类型包括: 容器序列 扁平序列 可变序列 不可变序列 容器序列 listtupledeque ...

  • Pickle 小结

    何为序列化 pickle可以序列(serializing)和反序列化(deserializing)python中的...

  • python列表

    本篇将介绍python中的列表,更多内容请参考:Python学习指南 一、序列 在python中有六种内建的序列:...

  • python学习_day8

    高级数据类型 序列:在python中,序列就是一组按照顺序排列的值【数据集合】 在python中存在三种内置的序列...

  • Python 入门之 内置模块 -- 序列化模块(json模块、

    Python 入门之 内置模块 -- 序列化模块(json模块、pickle模块) 1、序列化 Python中这种...

  • Day5——if_while_for

    一、if 语法: 二、for 语法: 说明: 序列 —— 属于序列的数据; python中的序列有:字符串、列表、...

  • Python起步——序列

    序列 在python种,最基本的数据结构就是序列。Python中包含6种内建的序列,分别是:列表、元组、字符串、U...

  • python - Lists

    2018-05-07 python - Lists Python中最基本的数据结构是序列。序列中的每个元素都被分配...

  • python中的序列

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

网友评论

      本文标题:python中的序列

      本文链接:https://www.haomeiwen.com/subject/zwudectx.html