标签:有用 append iss 计算 nbsp 设置 rev john ext
序列:(如列表和元组)在序列中,每个元素都有编号
映射:(如字典)在映射中,每个元素都有名称(也叫键)
>>> edward = [‘Edward Gumby‘, 42]
>>> john = [‘John Smith‘, 50]
>>> database = [edward, john]
>>> database
[[‘Edward Gumby‘, 42], [‘John Smith‘, 50]]
索引、切片、相加、相乘和成员资格检查。
>>> greeting = ‘Hello‘
>>> greeting[0]
‘H‘
>>> greeting[-1]
‘o‘
>>> ‘Hello‘[1]
‘e‘
>>> fourth = input(‘Year: ‘)[-4]
Year: 2005
>>> fourth
‘2‘
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[3:6]
[4, 5, 6]
(1)简写
>>> numbers[-3:]
[8, 9, 10]
>>> numbers[:3]
[1, 2, 3]
>>> numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(2)步长
>>> numbers[0:10:2]
[1, 3, 5, 7, 9]
>>> numbers[::4]
[1, 5, 9]
>>> numbers[8:3:-1]
[9, 8, 7, 6, 5]
>>> numbers[10:0:-2]
[10, 8, 6, 4, 2]
>>> numbers[0:10:-2]
[]
>>> numbers[::-2]
[10, 8, 6, 4, 2]
>>> numbers[5::-2]
[6, 4, 2]
>>> numbers[:5:-2]
[10, 8]
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> ‘Hello,‘ + ‘world!‘ #字符串拼接,”+“号每出现一次就会在内存中重新开辟一块空间
‘Hello,world!‘
>>> [1, 2, 3] + ‘world!‘
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
[1, 2, 3] + ‘world!‘
TypeError: can only concatenate list (not "str") to list
>>> ‘python‘ * 5
‘pythonpythonpythonpythonpython‘
>>> [42] * 10
[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
空列表是使用不包含任何内容的两个方括号([])表示的。
在Python中,None表示什么都没有。
列表的长度初始化为10,可像下面这样做:
>>> sequence = [None] * 10
>>> sequence
[None, None, None, None, None, None, None, None, None, None]
>>> permissions = ‘rw‘
>>> ‘w‘ in permissions
True
>>> ‘x‘ in permissions
False
>>> users = [‘mlh‘, ‘foo‘, ‘bar‘]
>>> input(‘Enter your user name: ‘) in users
Enter your user name: mlh
True
>>> ‘py‘ in ‘python‘
True
>>> ‘Py‘ in ‘python‘ #区分大小写
False
>>> [] #空列表
[]
>>> [1] #有一个元素的列表
[1]
>>> [1,2,3] #有多个元素的列表
[1, 2, 3]
>>>name_list=[‘alex‘, ‘seven‘, ‘eric‘]
[‘alex‘, ‘seven‘, ‘eric‘]
>>> hi=list(‘Hello‘)
>>> hi
[‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘]
>>> name_list=list([‘alex‘, ‘seven‘, ‘eric‘])
>>> name_list
[‘alex‘, ‘seven‘, ‘eric‘]
>>> num=list((1,2,3))
>>> num
[1, 2, 3]
可对列表执行所有的标准序列操作,如索引、切片、拼接和相乘
>>> x = [1, 1, 1]
>>> x[1] = 2
>>> x
[1, 2, 1]
>>> x[3]=4
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
x[3]=4
IndexError: list assignment index out of range
x[m:n] = [任意长度的序列]
>>> name = list(‘Perl‘)
>>> name[1:] = list(‘ython‘)
>>> name
[‘P‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘]
>>> name[1:4] =[] #给切片赋值一个空序列,相当于删除切片中的元素
>>> name
[‘P‘, ‘o‘, ‘n‘]
x[m:m] = [任意长度的序列]
>>> numbers = [1, 5]
>>> numbers[1:1] = [2, 3, 4]
>>> numbers
[1, 2, 3, 4, 5]
>>> names = [‘Alice‘, ‘Beth‘, ‘Cecil‘, ‘Dee-Dee‘, ‘Earl‘]
>>> del names[2]
>>> names
[‘Alice‘, ‘Beth‘, ‘Dee-Dee‘, ‘Earl‘]
>>> del names[1:3]
>>> names
[‘Alice‘, ‘Earl‘]
1、clear()
>>> lst = [1, 2, 3]
>>> lst.clear()
>>> lst
[]
2、copy()深浅
>>> a = [1,2,3]
>>> b = a #a和b指向同一个内存空间
>>> b[1] = 4
>>> a
[1, 4, 3]
>>> b
[1, 4, 3]
>>> c = [4,5,6]
>>> d = c.copy() #类似于使用a[:]或list(a),它们也都复制a。
>>> d[1] = 7
>>> c
[4, 5, 6]
>>> d
[4, 7, 6]
3、count()
>>> [‘to‘, ‘be‘, ‘or‘, ‘not‘, ‘to‘, ‘be‘].count(‘to‘)
2
>>> x = [[1, 2], 1, 1, [2, 1, [1, 2]]]
>>> x.count(1)
2
>>> x.count([1, 2])
1
4、index()
>>> knights = [‘We‘, ‘are‘, ‘the‘, ‘knights‘, ‘who‘, ‘say‘, ‘ni‘]
>>> knights.index(‘who‘)
4
>>> knights.index(‘herring‘)
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
knights.index(‘herring‘)
ValueError: ‘herring‘ is not in list
5、pop(n)
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x.pop()
9
>>> x
[1, 2, 3, 4, 5, 6, 7, 8]
>>> x.pop()
8
>>> x
[1, 2, 3, 4, 5, 6, 7]
>>> x.pop(2)
3
>>> x
[1, 2, 4, 5, 6, 7, 8]
6、append(x)
>>> lst = [1, 2, 3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a + b
[1, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3]
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a[len(a):] = b
>>> a
[1, 2, 3, 4, 5, 6]
8、insert
>>> numbers = [1, 2, 3, 5, 6, 7]
>>> numbers.insert(3, ‘four‘)
>>> numbers
[1, 2, 3, ‘four‘, 5, 6, 7]
>>> numbers = [1, 2, 3, 5, 6, 7]
>>> numbers[3:3] = [‘four‘]
>>> numbers
[1, 2, 3, ‘four‘, 5, 6, 7]
9、remove(元素)
>>> x = [‘to‘, ‘be‘, ‘or‘, ‘not‘, ‘to‘, ‘be‘]
>>> x.remove(‘be‘)
>>> x
[‘to‘, ‘or‘, ‘not‘, ‘to‘, ‘be‘]
>>> x.remove(‘bee‘)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
x.remove(‘bee‘)
ValueError: list.remove(x): x not in list
10、reverse()
>>> x = [1, 2, 3]
>>> x.reverse()
>>> x
[3, 2, 1]
11、sort()
>>> x = [4, 6, 2, 1, 7, 9]
>>> x.sort()
>>> x
[1, 2, 4, 6, 7, 9]
>>> x = [‘aardvark‘, ‘abalone‘, ‘acme‘, ‘add‘, ‘aerate‘]
>>> x.sort(key=len)
>>> x
[‘add‘, ‘acme‘, ‘aerate‘, ‘abalone‘, ‘aardvark‘]
>>> x = [4, 6, 2, 1, 7, 9]
>>> x.sort(reverse=True)
>>> x
[9, 7, 6, 4, 2, 1]
>>> () #空元组
()
>>> 1, #有一个元素的元组
(1,)
>>> 1,2,3 #有多个元素的元组
(1, 2, 3)
>>> (1,2,3)
(1, 2, 3)
>>> tuple(‘abc‘)
(‘a‘, ‘b‘, ‘c‘)
>>> tuple([1, 2, 3])
(1, 2, 3)
>>> tuple((1, 2, 3))
(1, 2, 3)
标签:有用 append iss 计算 nbsp 设置 rev john ext
原文地址:https://www.cnblogs.com/maiblogs/p/14304588.html