《Python for Beginners》为LearnStreet上的Python入门课程。本节主要学习内容为Dictionaries(字典)。
Lesson 7 Dictionaries
1. Indexing Dictionaries
查找字典练习
1 defrun():2 family = {"dad": 60, "mom" : 58, "brother": 20, "sister" : 15, "me" : 10}3 return family["brother"]4
5 #This is just for you to see what happens when the function is called
6 print run()
输出结果:
20
2. Creating Dictionaries
创建字典
1 defrun():2 knights = {"Arthur": "king", "Lancelot": "brave", "Galahad": "pure", "Robin": "not-quite-so-brave"}3 returnknights4
5 #This is just for you to see what happens when the function is called
6 print run()
输出结果:
{'Arthur': 'king', 'Galahad': 'pure', 'Robin': 'not-quite-so-brave', 'Lancelot': 'brave'}
3. Adding to Dictionaries
为字典添加元素
You can add a key/value pair to a dictionary with this syntax: dictionary_name[key] = value.
1 defrun():2 knights = {"Arthur":"king", "Lancelot":"brave", "Galahad":"pure", "Robin":"not-quite-so-brave"}3 knights["Bedivere"] = "wise"
4 returnknights5
6 #This is just for you to see what happens when the function is called
7 print run()
输出结果:
{'Arthur': 'king', 'Galahad': 'pure', 'Bedivere': 'wise', 'Robin': 'not-quite-so-brave', 'Lancelot': 'brave'}
4. Deleting from Dictionaries
从字典中删除一个元素。
1 defrun():2 knights = {"Arthur":"king", "Lancelot":"brave", "Galahad":"pure", "Robin":"not-quite-so-brave", "Bedivere":"wise"}3 #your code here
4 del knights["Galahad"]5 returnknights6
7 #This is just for you to see what happens when the function is called
8 print run()
输出结果:
{'Arthur': 'king', 'Bedivere': 'wise', 'Robin': 'not-quite-so-brave', 'Lancelot': 'brave'}
5. Iterating
1 >>> defrun(letters):2 keys =[]3 values =[]4 #your code here
5 for key inletters.keys():6 keys.append(key)7 for value inletters.values():8 values.append(value)9 return(keys, values)10
11 >>> print run({"a": 1, "b": 2, "c": 3, "d": 4})12 (['a', 'c', 'b', 'd'], [1, 3, 2, 4])
6. 复习
任务:Combine the two dictionaries into family and remove both exes. Don't forget to return family at the end.
1 >>> defrun():2 smiths = {"father": "Mike", "ex-wife" : "Mary", "children" : ["Bobby", "Susan"] }3 jones = {"mother": "Lucy", "ex-husband": "Peter", "children": ["Michelle", "Jeff", "Evan"]}4 family ={}5 for key insmiths:6 if key infamily:7 family[key]+=smiths[key]8 else:9 family[key]=smiths[key]10 for key injones:11 if key infamily:12 family[key]+=jones[key]13 else:14 family[key]=jones[key]15 keysToDel =[]16 for key infamily:17 if 'ex' inkey:18 keysToDel.append(key)19 printkeysToDel20 for key inkeysToDel:21 delfamily[key]22 returnfamily23
24 >>> printrun()25 ['ex-husband', 'ex-wife']26 {'father': 'Mike', 'children': ['Bobby', 'Susan', 'Michelle', 'Jeff', 'Evan'], 'mother': 'Lucy'}
7. Set Operations
计算两个集合的交集。
1 >>> defrun(first, second):2 #your code here
3 returnfirst.intersection(second)4
5 >>> run(set(range(11)), set(range(4)))6 set([0, 1, 2, 3])
8. Creating Sets
创建两个集合,并求它们的并集。
1 >>> defrun():2 set1 = {4, 6, 7, 9, 10, 12}3 set2 = {6, 9, 10, 12, 20}4 returnset1.union(set2)5
6 >>> printrun()7 set([4, 6, 7, 9, 10, 12, 20])