元组tuple
列表属于可变序列,可任意修改。而元组是不可变序列,定义之后就不能修改。
- 元组核心特点:不可变序列。
- 元组的访问和处理速度比列表快。
- 与整数和字符串一样,元组可以作为字典的键,列表则永远不能作为字典的键使用。
- ()小括号创建元组
一个元素时候,要加逗号“,”
>>> a=(1,2,3)
>>> a
(1, 2, 3)
>>> type(a)
<class 'tuple'>
>>> b=(1)
>>> type(b)
<class 'int'>
>>> c=(1,)
>>> type(c)
<class 'tuple'>
- tuple()创建元组
tuple(可迭代对象)
>>> a=tuple('hello')
>>> a
('h', 'e', 'l', 'l', 'o')
>>> b=list('hello')
>>> b
['h', 'e', 'l', 'l', 'o']
>>> c=tuple(range(4))
>>> c
(0, 1, 2, 3)
>>> d=tuple([6,7,8])
>>> d
(6, 7, 8)
>>> del a
>>> a
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
a
NameError: name 'a' is not defined
- 元组的元素访问和计数
元组元素不能修改!其余访问和计数与列表基本一致。
列表排序有自己的函数:list.sort()修改了原来列表元素;
而元组没有排序函数,所以要想排序只能使用内置函数sorted(tupleObj)。sorted()函数会创建新的列表(不管是元组还是列表,经过sorted()函数之后新建的都是列表)
>>> a=list(28,12,33,98,-2)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
a=list(28,12,33,98,-2)
TypeError: list expected at most 1 argument, got 5
>>> a=list([28,12,33,98,-2])
>>> a
[28, 12, 33, 98, -2]
>>> a=[28, 12, 33, 98, -2,6]
>>> type(a)
<class 'list'>
>>> a.sorted()
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
a.sorted()
AttributeError: 'list' object has no attribute 'sorted'
>>> a.sort()
>>> a
[-2, 6, 12, 28, 33, 98]
>>> a.sort(reverse=True)
>>> a
[98, 33, 28, 12, 6, -2]
>>> sorted(a)
[-2, 6, 12, 28, 33, 98]
>>> a
[98, 33, 28, 12, 6, -2]
>>> c
(0, 1, 2, 3)
>>> type(c)
<class 'tuple'> #这是元组tuple
>>> c.sort()
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
c.sort()
AttributeError: 'tuple' object has no attribute 'sort'
>>> sorted(c,reverse=True)
[3, 2, 1, 0] #这是个列表
>>> c
(0, 1, 2, 3)
>>>
- zip
zip(列表1,列表2,…)将多个列表对应位置的元素组合成元组,并返回zip对象。
>>> a
[98, 33, 28, 12, 6, -2]
>>> b
['h', 'e', 'l', 'l', 'o']
>>> zip(a,b)
<zip object at 0x1030ae480>
>>> a
[98, 33, 28, 12, 6, -2]
>>> b
['h', 'e', 'l', 'l', 'o']
>>> e=zip(a,b)
>>> e
<zip object at 0x1030ae480>
>>> list(e)
[(98, 'h'), (33, 'e'), (28, 'l'), (12, 'l'), (6, 'o')]
#a的最后一个-2没有在zip中,因为b只有5个元素,而a中的-2是第6个元素。
- 生成器推导式
>>> s=(x^2 for x in range(6))
>>> type(s)
<class 'generator'>
>>> t=list(s)
>>> t
[2, 3, 0, 1, 6, 7]
>>> q = tuple(s)
>>> q
()
>>> s=(x**2 for x in range(6))
>>> t=list(s)
>>> t
[0, 1, 4, 9, 16, 25]
>>> p=tuple(s)
>>> p
()
>>> s=(x**2 for x in range(6))
>>> s.__next__()
0
>>> s.__next__()
1
>>> s.__next__()
4
>>> s.__next__()
9
>>> s.__next__()
16
>>> s.__next__()
25
>>> s.__next__()
Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
s.__next__()
StopIteration
字典
字典是“键值对”的无序可变序列,字典中每个元素都包含一个键对象、一个值对象。
a={‘name’:‘hoesen’,‘age’:25,‘major’:‘cv’}
- 字典创建
1、花括号{}、dict()函数来创建字典对象
>>> b
{'name': 'gaoqi', 'age': 18}
>>> b.'name'
SyntaxError: invalid syntax
>>> b.get('name')
'gaoqi'
>>> b['name']
'gaoqi'
>>> a={'name':'hoesen','age':25,'major':'cv'}
>>> a
{'name': 'hoesen', 'age': 25, 'major': 'cv'}
>>> b={name:'hoesen',age:25,major:'cv'}
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
b={name:'hoesen',age:25,major:'cv'}
NameError: name 'name' is not defined
>>> a
{'name': 'hoesen', 'age': 25, 'major': 'cv'}
>>> b=dict(name:'hoesen',age:25,major:'cv')
SyntaxError: invalid syntax
>>> b=dict(name='hoesen',age=25,major='cv')
>>> b
{'name': 'hoesen', 'age': 25, 'major': 'cv'}
>>> c=dict([('name','hoesen'),('age',25)])
>>> c
{'name': 'hoesen', 'age': 25}
>>> d=dict([('name','hoesen'),('age',25)],('major','cv'))
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
d=dict([('name','hoesen'),('age',25)],('major','cv'))
TypeError: dict expected at most 1 argument, got 2
>>> d=dict([('name','hoesen'),('age',25),('major','cv')])
>>> d
{'name': 'hoesen', 'age': 25, 'major': 'cv'}
>>> c={}
>>> c
{}
>>> d=dict()
>>> d
{}
2、zip()创建字典对象
>>> k=['name','age','major']
>>> v=['hoesen',25,'cv']
>>> a=zip(k,v)
>>> a
<zip object at 0x102dcc380>
>>> d=dict(a)
>>> d
{'name': 'hoesen', 'age': 25, 'major': 'cv'}
>>> d=dict(zip(k,v))
>>> d
{'name': 'hoesen', 'age': 25, 'major': 'cv'}
3、fromkeys()创建value为空的字典
>>> a=dict.fromkeys('name','age','major')
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
a=dict.fromkeys('name','age','major')
TypeError: fromkeys expected at most 2 arguments, got 3
>>> a=dict.fromkeys(['name','age','major'])
>>> a
{'name': None, 'age': None, 'major': None}