python学习笔记——当索引行不通时
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习笔记——当索引行不通时相关的知识,希望对你有一定的参考价值。
字典(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学习笔记——当索引行不通时的主要内容,如果未能解决你的问题,请参考以下文章