您的位置:首页 > 编程语言 > Python开发

《Python for Beginners》学习笔记(6)

2013-03-18 18:28 381 查看
《Python for Beginners》为LearnStreet上的Python入门课程。本节主要学习内容为Dictionaries(字典)。

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])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: