序列类型通用操作
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]
>>>
>>>
|