Python development basics, common usage dictionary, does not skills

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/meiguanxi7878/article/details/102726773

How to access an element in the dictionary

Create a dictionary student_name, if the key was not found, the program will error, error can be solved by exception handling, it will be described in detail later in what is exception handling, first use setdefault here today to solve the problem and get the error, see example :

student_name = {20190101: "王一", 20190102: "王二", 20190103: "王三", 20190104: "王四"}
print(student_name.setdefault(20190105, "None"))
print(student_name.get(20190105, "None"))


Dictionary access

Explanation: The key 20,190,105 find in the dictionary, does not exist, given the default key is None; similar to Python dictionary setdefault () function and get () method, if the key does not exist in the dictionary, and will add value to the key Defaults.

Dictionary access

Another way is to delete the dictionary Python dictionaries pop () method and the key corresponding to a given key value, the return value is deleted. key value must be given. Otherwise, return default values.

print(student_name.pop(20190105,"None"))


Explanation: absence deleted using pop key, if there is no key, default return value None, the latter parameter is to avoid obtaining an initial value of a key not present a

A common method dictionary

1) can not be used in the dictionary * +, you can use the update lets merge two dictionaries, see the demo map:

在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容

dic01={20190101:87,20190102:77,20190103:94,20190104:75}
dic02={20190105:90,20190106:89,20190107:95,20190108:76}
# print(dic01 + dic02) 
# print(dic01 * 3) 


Dictionary usage

dict usage

Use the update so that the two dictionaries merger, similar to +

dic01.update(dic02)
print(dic01)


The combined dictionary

2) assignment dictionary =

dic01={20190101:87,20190102:77,20190103:94,20190104:75}

dic03 = dic01

print(dic03)


Dictionary assignment

Note: If you change dic01, then dic03 also capricious.

  1. Dictionary copy () function returns a shallow copy of the dictionary
dic01={20190101:87,20190102:77,20190103:94,20190104:75}
dic04 = dic01.copy()
print(dic01)
print(dic04)
dic01[20190102] = 99
print(dic01)
print(dic04)


A shallow copy in the dictionary, the index value stored in each copy, a change, a further constant

4) len count the number of dictionary elements, i.e., the total number of keys.

在学习过程中有什么不懂得可以加我的
python学习交流扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
dic01={20190101:87,20190102:77,20190103:94,20190104:75}
print(len(dic01))


Number of elements in the dictionary

5) in determining whether the key contained in the dictionary

dic01={20190101:87,20190102:77,20190103:94,20190104:75}
print(20190102 in dic01)


Dictionary usage

6) sorted according to the sort key

dic01={20190104: 87, 20190102: 77, 20190101: 94, 20190103: 75}
print(sorted(dic01))


Dictionary usage

7)字典中针对key计算 max(最大值),min(最小值),sum(求和)

dic01={20190104:87,20190102:77,20190101:94,20190103:75}
print(max(dict01))
print(min(dict01))
print(sum(dict01))



对Python感兴趣或者是正在学习的小伙伴,可以加入我们的Python学习扣qun:784758214,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

字典的计算

在字典中使用fromkeys ()函数创建新字典,产生一个字典的架构

student_number=[20190101,20190102,20190103,]
student_result={}.fromkeys(student_number)#创建一个字典架构
print(student_result)
student_result[20190101]=258#添加value
student_result[20190102]=320
student_result[20190103]=347
print(student_result)


Guess you like

Origin blog.csdn.net/meiguanxi7878/article/details/102726773