《Python for Beginners》学习笔记(6)
2013-03-18 18:28
381 查看
《Python for Beginners》为LearnStreet上的Python入门课程。本节主要学习内容为Dictionaries(字典)。
Lesson 7 Dictionaries
1. Indexing Dictionaries
查找字典练习
输出结果:
20
2. Creating Dictionaries
创建字典
输出结果:
{'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.
输出结果:
{'Arthur': 'king', 'Galahad': 'pure', 'Bedivere': 'wise', 'Robin': 'not-quite-so-brave', 'Lancelot': 'brave'}
4. Deleting from Dictionaries
从字典中删除一个元素。
输出结果:
{'Arthur': 'king', 'Bedivere': 'wise', 'Robin': 'not-quite-so-brave', 'Lancelot': 'brave'}
5. Iterating
6. 复习
任务:Combine the two dictionaries into family and remove both exes. Don't forget to return family at the end.
7. Set Operations
计算两个集合的交集。
8. Creating Sets
创建两个集合,并求它们的并集。
Lesson 7 Dictionaries
1. Indexing Dictionaries
查找字典练习
def run(): family = {"dad": 60, "mom" : 58, "brother": 20, "sister" : 15, "me" : 10} return family["brother"] #This is just for you to see what happens when the function is called print run()
输出结果:
20
2. Creating Dictionaries
创建字典
def run(): knights = {"Arthur": "king", "Lancelot": "brave", "Galahad": "pure", "Robin": "not-quite-so-brave"} return knights #This is just for you to see what happens when the function is called 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.
def run(): knights = {"Arthur":"king", "Lancelot":"brave", "Galahad":"pure", "Robin":"not-quite-so-brave"} knights["Bedivere"] = "wise" return knights #This is just for you to see what happens when the function is called print run()
输出结果:
{'Arthur': 'king', 'Galahad': 'pure', 'Bedivere': 'wise', 'Robin': 'not-quite-so-brave', 'Lancelot': 'brave'}
4. Deleting from Dictionaries
从字典中删除一个元素。
def run(): knights = {"Arthur":"king", "Lancelot":"brave", "Galahad":"pure", "Robin":"not-quite-so-brave", "Bedivere":"wise"} #your code here del knights["Galahad"] return knights #This is just for you to see what happens when the function is called print run()
输出结果:
{'Arthur': 'king', 'Bedivere': 'wise', 'Robin': 'not-quite-so-brave', 'Lancelot': 'brave'}
5. Iterating
>>> def run(letters): keys = [] values = [] #your code here for key in letters.keys(): keys.append(key) for value in letters.values(): values.append(value) return (keys, values) >>> print run({"a": 1, "b": 2, "c": 3, "d": 4}) (['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.
>>> def run(): smiths = {"father": "Mike", "ex-wife" : "Mary", "children" : ["Bobby", "Susan"] } jones = {"mother": "Lucy", "ex-husband": "Peter", "children": ["Michelle", "Jeff", "Evan"]} family = {} for key in smiths: if key in family: family[key]+=smiths[key] else: family[key]=smiths[key] for key in jones: if key in family: family[key]+=jones[key] else: family[key]=jones[key] keysToDel = [] for key in family: if 'ex' in key: keysToDel.append(key) print keysToDel for key in keysToDel: del family[key] return family >>> print run() ['ex-husband', 'ex-wife'] {'father': 'Mike', 'children': ['Bobby', 'Susan', 'Michelle', 'Jeff', 'Evan'], 'mother': 'Lucy'}
7. Set Operations
计算两个集合的交集。
>>> def run(first, second): #your code here return first.intersection(second) >>> run(set(range(11)), set(range(4))) set([0, 1, 2, 3])
8. Creating Sets
创建两个集合,并求它们的并集。
>>> def run(): set1 = {4, 6, 7, 9, 10, 12} set2 = {6, 9, 10, 12, 20} return set1.union(set2) >>> print run() set([4, 6, 7, 9, 10, 12, 20])
相关文章推荐
- 《Python for Beginners》学习笔记(2)
- 《Python for Beginners》学习笔记(3)
- 《Python for Beginners》学习笔记(7)暨结课总结
- 《Python for Beginners》学习笔记(5)
- 《Python for Beginners》学习笔记(1)
- 《Python for Beginners》学习笔记(4)
- 《How to Use the TimeDistributed Layer for Long Short-Term Memory Networks in Python》学习笔记
- 《Python for kids》学习笔记(五)
- Python – An overview for beginners
- 《Python for kids》学习笔记(二)
- 《Python for kids》学习笔记(一)
- 《Python for kids》学习笔记(三)
- 《Python for kids》学习笔记(四)
- Python(四)之Python流程控制(if、while、for)
- ubuntu配置python for android开发环境的日志
- huffman for python
- Python for .NET
- HOW TO: Install Selenium 2 for Mac OS X / Python
- Python for Selenium: build test environment
- python之r.raise_for_status()