IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python内置类型序列类型通用操作使用示例 -> 正文阅读

[Python知识库]Python内置类型序列类型通用操作使用示例

序列类型通用操作

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> s = "abcdefg01234567"
>>> seq = s
>>> seq
'abcdefg01234567'
>>> "a" in seq
True
>>> "X" not in seq
True
>>> "a" not in seq
False
>>> "X" in seq
False
>>> "0" in seq
True
>>> "0" in seq
True
>>> "8" in seq
False
>>> "3" not in seq
False
>>> "9" not in seq
True
>>> 
>>> seq + "GHJ"
'abcdefg01234567GHJ'
>>> "KLJ" + seq
'KLJabcdefg01234567'
>>> seq * 3
'abcdefg01234567abcdefg01234567abcdefg01234567'
>>> 4*seq
'abcdefg01234567abcdefg01234567abcdefg01234567abcdefg01234567'
>>> seq[4]
'e'
>>> seq[1:7]
'bcdefg'
>>> seq[1:7:2]
'bdf'
>>> len(seq)
15
>>> min(seq)
'0'
>>> max(seq)
'g'
>>> seq.index("f")
5
>>> seq.index("F")
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    seq.index("F")
ValueError: substring not found
>>> seq.count("f")
1
>>> seq.count("F")
0
>>> 
>>> 
>>> ##############################
>>> ls = list(range(15))
>>> ls
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> seq = ls
>>> 3 in seq
True
>>> 17 in seq
False
>>> 3 not in seq
False
>>> 199 not in seq
True
>>> seq + [99,88]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 99, 88]
>>> [7777,9999] + seq
[7777, 9999, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> 
>>> seq
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> seq*2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> 3*seq
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> s[3]
'd'
>>> seq[3]
3
>>> seq[3:8]
[3, 4, 5, 6, 7]
>>> seq[2:13:3]
[2, 5, 8, 11]
>>> len(seq)
15
>>> min(seq)
0
>>> len (seq)
15
>>> max(seq)
14
>>> seq.index(4)
4
>>> s.count(999)
Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    s.count(999)
TypeError: must be str, not int
>>> seq.count(999)
0
>>> seq.count(3)
1
>>> seq
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> tp = tuple()
>>> tp
()
>>> tp = tuple(range(15))
>>> tp
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
>>> seq = tp
>>> seq
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
>>> 3 in seq
True
>>> 99 in seq
False
>>> 2 not in seq
False
>>> 999 not in seq
True
>>> seq + (99,88,77)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 99, 88, 77)
>>> seq
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
>>> (99,88,77) + seq
(99, 88, 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
>>> seq * 2
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
>>> 3*seq
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
>>> seq
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
>>> seq[4]
4
>>> seq[2:8]
(2, 3, 4, 5, 6, 7)
>>> seq[1:13:4]
(1, 5, 9)
>>> len(seq)
15
>>> min(seq)
0
>>> max(seq)
14
>>> s.index(4)
Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    s.index(4)
TypeError: must be str, not int
>>> seq.index(4)
4
>>> seq.index(555)
Traceback (most recent call last):
  File "<pyshell#80>", line 1, in <module>
    seq.index(555)
ValueError: tuple.index(x): x not in tuple
>>> seq.count(3333)
0
>>> seq.count(3)
1
>>> 
>>> 

可变序列类型通用操作

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls[0] = 'aa'
>>> ls
['aa', 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls[3:6] = ["a","b","c"]
>>> ls
[1000, 1001, 1002, 'a', 'b', 'c', 1006, 1007, 1008, 1009]
>>> len(ls)
10
>>> ls = [i+1000 for i in range(10)]
>>> len(ls)
10
>>> ls[3:6] = ["a","b","c",'d','e']
>>> ls
[1000, 1001, 1002, 'a', 'b', 'c', 'd', 'e', 1006, 1007, 1008, 1009]
>>> len(ls)
12
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> len(ls)
10
>>> ls[3:6] = ["a","b"]
>>> ls
[1000, 1001, 1002, 'a', 'b', 1006, 1007, 1008, 1009]
>>> len(ls)
9
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> del ls[3:6]
>>> ls
[1000, 1001, 1002, 1006, 1007, 1008, 1009]
>>> len(ls)
7
>>> ls = [i+1000 for i in range(10)]
>>> ls[3:6] = []
>>> ls
[1000, 1001, 1002, 1006, 1007, 1008, 1009]
>>> len(ls)
7
>>> ls = [i+1000 for i in range(10)]
>>> del ls[0:7:2]
>>> ls
[1001, 1003, 1005, 1007, 1008, 1009]
>>> len(ls)
6
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls[0:7:2] = []
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    ls[0:7:2] = []
ValueError: attempt to assign sequence of size 0 to extended slice of size 4
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.append(9999)
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 9999]
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.append([888888])
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, [888888]]
>>> ls.clear()
>>> ls
[]
>>> len(ls)
0
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> len(ls)
10
>>> ls.clear()
>>> ls
[]
>>> len(ls)
0
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> lsA = ls.copy()
>>> lsB = ls[:]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> lsA
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> lsB
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls[0] = 99999
>>> lsA[1] = 888888
>>> lsB[2] = 77777777
>>> ls
[99999, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> lsA
[1000, 888888, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> lsB
[1000, 1001, 77777777, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls = [i+1000 for i in range(10)]
>>> 
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.extend([9999,8888])
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 9999, 8888]
>>> len(ls)
12
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls += [555,333]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 555, 333]
>>> len(ls)
12
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls*=2
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> id(ls)
2552108555720
>>> llss = ls
>>> ls *= 2
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> id(ls)
2552108555720
>>> llss
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> id(llss)
2552108555720
>>> 
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.insert(3,"AAA")
>>> ls
[1000, 1001, 1002, 'AAA', 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> len(ls)
11
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.pop()
1009
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008]
>>> ls.pop()
1008
>>> ls.pop(-1)
1007
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006]
>>> ls.pop(-1)
1006
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005]
>>> ls.pop(-2)
1004
>>> ls
[1000, 1001, 1002, 1003, 1005]
>>> ls.pop(2)
1002
>>> ls
[1000, 1001, 1003, 1005]
>>> len(ls)
4
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.pop(0)
1000
>>> ls
[1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.pop(0)
1001
>>> ls
[1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.pop(10)
Traceback (most recent call last):
  File "<pyshell#124>", line 1, in <module>
    ls.pop(10)
IndexError: pop index out of range
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.pop(122)
Traceback (most recent call last):
  File "<pyshell#127>", line 1, in <module>
    ls.pop(122)
IndexError: pop index out of range
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.reverse()
>>> ls
[1009, 1008, 1007, 1006, 1005, 1004, 1003, 1002, 1001, 1000]
>>> len(ls)
10
>>> ls.reverse()
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> lsls = ls
>>> lsls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.reverse()
>>> ls
[1009, 1008, 1007, 1006, 1005, 1004, 1003, 1002, 1001, 1000]
>>> lsls
[1009, 1008, 1007, 1006, 1005, 1004, 1003, 1002, 1001, 1000]
>>> 
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.remove(1002)
>>> ls
[1000, 1001, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.remove(1002)
Traceback (most recent call last):
  File "<pyshell#149>", line 1, in <module>
    ls.remove(1002)
ValueError: list.remove(x): x not in list
>>> ls.remove(1009)
>>> ls
[1000, 1001, 1003, 1004, 1005, 1006, 1007, 1008]
>>> ls = [i+1000 for i in range(10)]
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.remove(1010)
Traceback (most recent call last):
  File "<pyshell#154>", line 1, in <module>
    ls.remove(1010)
ValueError: list.remove(x): x not in list
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]
>>> ls.remove(1009)
>>> ls
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008]
>>> 
>>> 
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-12-18 15:55:49  更:2021-12-18 15:56:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 5:39:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码