新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:如何使用python 字典添加數(shù)據(jù)?
關(guān)于python添加的內(nèi)容并不少,大部分小伙伴都非常喜歡用python去添加內(nèi)容,根據(jù)大家的喜愛程度,給大家又準(zhǔn)備了關(guān)于添加數(shù)據(jù)的內(nèi)容,就是使用字典,小伙伴想不想了解?接下來看下面內(nèi)容哦~

首先新建一個(gè)python文件命名為py3_dict.py,在這個(gè)文件中進(jìn)行字符串操作代碼編寫(如下為代碼,文后有顯示運(yùn)行效果):
#dictionaries 是一個(gè)Key-Value對(duì)形式的集合
#定義一個(gè)字典
student = {'name':'yale','age':25,'course':['數(shù)學(xué)','計(jì)算機(jī)']}
print(student)
print(student['name'])
print(student['course'])
#字典的key和value可定義為immutable data type
#例如:定義key為1
student = {1:'yale','age':25,'course':['數(shù)學(xué)','計(jì)算機(jī)']}
print(student[1])
#訪問一個(gè)不存在的key
#會(huì)出現(xiàn)異常
#KeyError: 'phone'
student = {'name':'yale','age':25,'course':['數(shù)學(xué)','計(jì)算機(jī)']}
#print(student['phone'])
#有時(shí)候我們希望不存在的key
#可以返回None或者一個(gè)默認(rèn)值
#用如下方式實(shí)現(xiàn):
print(student.get('phone'))#None
print(student.get('phone','未找到'))#返回默認(rèn)值:未找到
#往dict字典中添加數(shù)據(jù)
student = {'name':'yale','age':25,'course':['數(shù)學(xué)','計(jì)算機(jī)']}
student['phone']='010-55555555'
print(student.get('phone','未找到'))#010-55555555
#改變已存在的key對(duì)應(yīng)的值
student = {'name':'yale','age':25,'course':['數(shù)學(xué)','計(jì)算機(jī)']}
student['name']='andy'
print(student)
#使用update() 改變字典中的多個(gè)值
student = {'name':'yale','age':25,'course':['數(shù)學(xué)','計(jì)算機(jī)']}
student.update({'name':'andy','age':26,'phone':'12345678'})
print(student)
#刪除一個(gè)key
#使用del 關(guān)鍵字
del student['phone']
print(student)
#或者使用之前提到過的pop()方法
#刪除數(shù)據(jù)
age = student.pop('age')
print(age)#26
print(student)
#使用len()查看字典中一共有多少key
student = {'name':'yale','age':25,'course':['數(shù)學(xué)','計(jì)算機(jī)']}
print(len(student))#3
#查看所有的key
print(student.keys())#dict_keys(['name', 'age', 'course'])
#查看所有的value
print(student.values())#dict_values(['yale', 25, ['數(shù)學(xué)', '計(jì)算機(jī)']])
#查看所有的key和value
#得到一對(duì)一對(duì)的key-value
#dict_items([('name', 'yale'), ('age', 25), ('course', ['數(shù)學(xué)', '計(jì)算機(jī)'])])
print(student.items())
#循環(huán)字典
#像list的方式循環(huán),打印的是key值
#name
#age
#course
for key in student:
print(key)
#所以我們用items()方法循環(huán)數(shù)據(jù):
for key,value in student.items():
print(key,value)
#結(jié)果:
#name yale
#age 25
#course ['數(shù)學(xué)', '計(jì)算機(jī)']以上代碼運(yùn)行效果:
{'name': 'yale', 'age': 25, 'course': ['數(shù)學(xué)', '計(jì)算機(jī)']}
yale
['數(shù)學(xué)', '計(jì)算機(jī)']
yale
None
未找到
010-55555555
{'name': 'andy', 'age': 25, 'course': ['數(shù)學(xué)', '計(jì)算機(jī)']}
{'name': 'andy', 'age': 26, 'course': ['數(shù)學(xué)', '計(jì)算機(jī)'], 'phone': '12345678'}
{'name': 'andy', 'age': 26, 'course': ['數(shù)學(xué)', '計(jì)算機(jī)']}
26
{'name': 'andy', 'course': ['數(shù)學(xué)', '計(jì)算機(jī)']}
3
dict_keys(['name', 'age', 'course'])
dict_values(['yale', 25, ['數(shù)學(xué)', '計(jì)算機(jī)']])
dict_items([('name', 'yale'), ('age', 25), ('course', ['數(shù)學(xué)', '計(jì)算機(jī)'])])
name
age
course
name yale
age 25
course ['數(shù)學(xué)', '計(jì)算機(jī)']好啦,如果項(xiàng)目里需要用到以上內(nèi)容,大家可以參考上面示例,根據(jù)自己的需求,設(shè)計(jì)代碼哦~
新聞標(biāo)題:創(chuàng)新互聯(lián)Python教程:如何使用python 字典添加數(shù)據(jù)?
URL網(wǎng)址:http://fisionsoft.com.cn/article/djhosse.html


咨詢
建站咨詢
