《Python for Beginners》为LearnStreet上的Python入门课程。本节主要学习内容为Dictionaries(字典)。
Lesson 7 Dictionaries
1. Indexing Dictionaries
查找字典练习
1 def run(): 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 def run(): 2 knights = {"Arthur": "king", "Lancelot": "brave", "Galahad": "pure", "Robin": "not-quite-so-brave"} 3 return knights 4 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 def run(): 2 knights = {"Arthur":"king", "Lancelot":"brave", "Galahad":"pure", "Robin":"not-quite-so-brave"} 3 knights["Bedivere"] = "wise" 4 return knights 5 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 def run(): 2 knights = {"Arthur":"king", "Lancelot":"brave", "Galahad":"pure", "Robin":"not-quite-so-brave", "Bedivere":"wise"} 3 #your code here 4 del knights["Galahad"] 5 return knights 6 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 >>> def run(letters): 2 keys = [] 3 values = [] 4 #your code here 5 for key in letters.keys(): 6 keys.append(key) 7 for value in letters.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 >>> def run(): 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 in smiths: 6 if key in family: 7 family[key]+=smiths[key] 8 else: 9 family[key]=smiths[key] 10 for key in jones: 11 if key in family: 12 family[key]+=jones[key] 13 else: 14 family[key]=jones[key] 15 keysToDel = [] 16 for key in family: 17 if 'ex' in key: 18 keysToDel.append(key) 19 print keysToDel 20 for key in keysToDel: 21 del family[key] 22 return family 23 24 >>> print run() 25 ['ex-husband', 'ex-wife'] 26 {'father': 'Mike', 'children': ['Bobby', 'Susan', 'Michelle', 'Jeff', 'Evan'], 'mother': 'Lucy'}
7. Set Operations
计算两个集合的交集。
1 >>> def run(first, second): 2 #your code here 3 return first.intersection(second) 4 5 >>> run(set(range(11)), set(range(4))) 6 set([0, 1, 2, 3])
8. Creating Sets
创建两个集合,并求它们的并集。
1 >>> def run(): 2 set1 = {4, 6, 7, 9, 10, 12} 3 set2 = {6, 9, 10, 12, 20} 4 return set1.union(set2) 5 6 >>> print run() 7 set([4, 6, 7, 9, 10, 12, 20])