python第三天字典表及元组

#字典表的声明
d={‘BN’:‘223654’,‘title’:‘python入门’,‘price’:‘39’}
d
{‘BN’: ‘223654’, ‘title’: ‘python入门’, ‘price’: ‘39’}

d[‘price’]
‘39’

d[‘a加粗样式uthor’]=‘Bob’
d
{‘BN’: ‘223654’, ‘title’: ‘python入门’, ‘price’: ‘39’, ‘author’: ‘Bob’}

#与列表不同的是不存在的键,通过赋值直接加入进去
d[‘Author’]
Traceback (most recent call last):
File “<pyshell#7>”, line 1, in
d[‘Author’]
KeyError: ‘Author’

#获取键值用get,若不存在不会抛异常
d.get(‘price’)
‘39’

d.(‘Author’)
SyntaxError: invalid syntax

d.get(‘Author’)
d.get(‘Author’,0.0)
0.0

#没有则返回一个默认值0.0
emp=dict(name=‘Mike’,age=20,job=‘dev’)
#通过构造函数来构建
emp
{‘name’: ‘Mike’, ‘age’: 20, ‘job’: ‘dev’}

s=‘tom’
s[0]=‘g’
Traceback (most recent call last):
File “<pyshell#18>”, line 1, in
s[0]=‘g’
TypeError: ‘str’ object does not support item assignment

#字符串不支持原位改变,列表支持
#字典表也支持原位改变
emp[‘name’]=‘jack’
emp
{‘name’: ‘jack’, ‘age’: 20, ‘job’: ‘dev’}

dep={‘department’,‘技术部’}
emp.update(dep)
Traceback (most recent call last):
File “<pyshell#24>”, line 1, in
emp.update(dep)
ValueError: dictionary update sequence element #0 has length 3; 2 is required

dep={‘department’:‘技术部’}
emp.update(dep)
emp
{‘name’: ‘jack’, ‘age’: 20, ‘job’: ‘dev’, ‘department’: ‘技术部’}

#通过update将dep加入emp
emp.keys()
dict_keys([‘name’, ‘age’, ‘job’, ‘department’])

emp.values
<built-in method values of dict object at 0x000001CD5291DE58>

emp.values ()
dict_values([‘jack’, 20, ‘dev’, ‘技术部’])

#注意不是虽然是列表的形式,但它不是列表,遍历可以用for来遍历
for k in emp.keys()
SyntaxError: invalid syntax

for k in emp.keys():
print(k)
name
age
job
department

#字典表在定义时,他的键要是不支持原位改变的类型,如字符串可以,列表不行
#{[1,2,2]:’’}不行,但元组可以
for k,v in emp.items():
print(’{}=>{}’.format(k,v))
name=>jack
age=>20
job=>dev
department=>技术部

emp={‘age’:20,‘name’:{‘firstname’:‘jerry’,‘lastname’:‘lee’}}
emp
{‘age’: 20, ‘name’: {‘firstname’: ‘jerry’, ‘lastname’: ‘lee’}}

#和列表一样支持嵌套
#sort()是列表特有的函数,而sorted()全局函数支持所有可迭代的类型,所以字典的键值若要排序可先变为list利用sort()函数,或直接用sorted()
d={‘a’:1,‘b’:2,‘c’:3,‘d’:4}
d
{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}

d
{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}

d
{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}

d
{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}

ks=list(d.kes())
Traceback (most recent call last):
File “<pyshell#51>”, line 1, in
ks=list(d.kes())
AttributeError: ‘dict’ object has no attribute ‘kes’

ks=list(d.keys())
ks.sort()
ks
[‘a’, ‘b’, ‘c’, ‘d’]

for k in ks:
print(d.get(k))
1
2
3
4

ks=d.keys()
ks
dict_keys([‘a’, ‘b’, ‘c’, ‘d’])

for k in sorted(ks):
print(k,d.get(k))
a 1
b 2
c 3
d 4

#元组
(1,2)
SyntaxError: invalid character in identifier

(1,2)
(1, 2)

(1)+(2)
3

(1,2)+(2,3)
(1, 2, 2, 3)

x=20,
x
(20,)

len(x)
1

t=(1,2,3,4)
t[0]=22
Traceback (most recent call last):
File “<pyshell#72>”, line 1, in
t[0]=22
TypeError: ‘tuple’ object does not support item assignment

for k in t:
print(k)
1
2
3
4

#不支持原位改变,支持遍历,支持推导
res=[x**2 for x in t]
res
[1, 4, 9, 16]

#遍历元组t中的值,返回值的平方,以列表的形式表现
t.index(2)
1

from collections import namedtuple
employee=namedtuple(‘employee’,[‘name’,‘age’,‘department’,‘salary’])
#定义一个模板
jerry=employee(‘jerry’,age=30,department=‘财务部’,salary=9000)
jerry.name
‘jerry’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值