1. 程式人生 > >python學習筆記——當索引行不通時

python學習筆記——當索引行不通時

dict函數操作

字典(dict)
phonebook={‘cc‘:‘12334‘,‘dd‘:‘123443‘}

>> phonebook(‘cc‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘dict‘ object is not callable
>> phonebook(cc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>

NameError: name ‘cc‘ is not defined
>> s=dict(phonebook)
>> s
{‘cc‘: ‘12334‘, ‘dd‘: ‘123443‘}
>> s(cc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘cc‘ is not defined
>> s(‘cc‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘dict‘ object is not callable
>> phonebook
{‘cc‘: ‘12334‘, ‘dd‘: ‘123443‘}
>> phonebook(‘cc‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘dict‘ object is not callable

phonebook[‘cc‘]
‘12334‘
dict函數:從其它映射或鍵——值對序列創建字典
items=[(‘name‘,lx),(‘age‘,26)]

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘lx‘ is not defined

>> items=[(‘name‘,‘lx‘),(‘age‘,26)]
>> c=dict(items)
>> c
{‘name‘: ‘lx‘, ‘age‘: 26}

d=dict()

>> d
{}
如果未定義字典將返回空字典

dict基本操作
len(d):返回d包含的項數
d[k]:返回d中第k項值
d[k]=v:給d中第K項賦值
del d[k]:刪除
k in d:查詢在d中是否有k鍵
dict中沒有的元素也可以通過外部直接賦值添加,不像list需要append

>> d[10]=‘kk‘
>> d
{10: ‘kk‘}

>> x=[]
>> x[30]=‘kk‘
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>> x={}
>> x[30]=‘kk‘
>> x
{30: ‘kk‘}
書上練習的一小段
people={
‘lx‘:{
‘phone‘:‘123‘,
‘addr‘:‘fdfsf‘
},
‘sdy‘:{
‘phone‘:‘456‘,
‘addr‘:‘test‘
},
‘sb‘:{
‘phone‘:‘789‘,
‘addr‘:‘test1‘
}

}
labels={‘phone‘:‘phonenumber‘,
‘addr‘:‘address‘
}
name=input(‘Name:‘)
r=input(‘Please input number(p) or address(a)‘)
if r==‘p‘:key=‘phone‘
if r==‘a‘:key=‘addr‘
if name in people:print("{}‘s {} is {}".format(name,labels[key],people[name][key]))
字符串格式功能用於dict:format_map()
查詢關鍵字對應替換

字典方法:
clear:清除dict內所有項

>> d={}
>> d
{}
>> d{‘name‘}=‘lx‘
File "<stdin>", line 1
d{‘name‘}=‘lx‘
^
SyntaxError: invalid syntax
>> d[‘name‘]=‘lx‘
>> d
{‘name‘: ‘lx‘}
>> c{}=d.clear()
File "<stdin>", line 1
c{}=d.clear()
^
SyntaxError: invalid syntax
>> c{}.clear()
File "<stdin>", line 1
c{}.clear()
^
SyntaxError: invalid syntax
>> c=d.clear()
>> c
>> d
{}
copy:從A dict復制到B dict
>> x={‘name‘:‘lx‘,‘age‘:‘35‘,‘height‘:‘170‘}
>> y=x.copy()
>> y
{‘name‘: ‘lx‘, ‘age‘: ‘35‘, ‘height‘: ‘170‘}####淺復制
采用替換副本中的值的方式原件不會收到影響,如果就地修改副本中的值原件也會修改
>> x={‘name‘:‘lx‘,‘age‘:‘35‘,‘height‘:[‘170‘,‘180‘,‘190‘]}
>> x
{‘name‘: ‘lx‘, ‘age‘: ‘35‘, ‘height‘: [‘170‘, ‘180‘, ‘190‘]}
>> y
{‘name‘: ‘xx‘, ‘age‘: ‘35‘, ‘height‘: ‘170‘}
>> y=x.copy()
>> y
{‘name‘: ‘lx‘, ‘age‘: ‘35‘, ‘height‘: [‘170‘, ‘180‘, ‘190‘]}
>> y[‘height‘].remove(‘190‘)
>> y
{‘name‘: ‘lx‘, ‘age‘: ‘35‘, ‘height‘: [‘170‘, ‘180‘]}
>> x
{‘name‘: ‘lx‘, ‘age‘: ‘35‘, ‘height‘: [‘170‘, ‘180‘]}
>> y[‘name‘]=‘cc‘
>> y
{‘name‘: ‘cc‘, ‘age‘: ‘35‘, ‘height‘: [‘170‘, ‘180‘]}
>> x
{‘name‘: ‘lx‘, ‘age‘: ‘35‘, ‘height‘: [‘170‘, ‘180‘]}

deepcopy:深復制,副本中修改不影響原件

>> from copy import deepcopy
>> d={}
>> d
{}
>> d[‘name‘]=[‘lx‘,‘cc‘]
>> d
{‘name‘: [‘lx‘, ‘cc‘]}
>> c=d.copy()
>> c
{‘name‘: [‘lx‘, ‘cc‘]}
>> dc=d.deepcopy()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ‘dict‘ object has no attribute ‘deepcopy‘
>> dc=deepcopy(d)
>> d[‘name‘].append(‘dd‘)
>> c
{‘name‘: [‘lx‘, ‘cc‘, ‘dd‘]}
>> dc
{‘name‘: [‘lx‘, ‘cc‘]}
>>

fromkey:創建一個新dict,且其中的項都為none

>> dict.fromkeys([‘name‘,‘age‘,‘height‘])
{‘name‘: None, ‘age‘: None, ‘height‘: None}

get:提供寬松的環境

>> c={}
>> print(c[‘name‘])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: ‘name‘
>> print(c.get(‘name‘))
>> c.get(‘name‘,N/A)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘N‘ is not defined
>> c.get(‘name‘,‘N/A‘)
‘N/A‘

python學習筆記——當索引行不通時