首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

输出会合《Python for Beginners》学习笔记(6) 输出集合

2013-04-21 
输出集合《Python for Beginners》学习笔记(6) 输出集合在改章节中,我们重要介绍输出集合的容内,自我感觉有

输出集合《Python for Beginners》学习笔记(6) 输出集合

在改章节中,我们重要介绍输出集合的容内,自我感觉有个不错的建议和大家分享下

????《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 called6 print run()

输出结果:
20

????2. Creating Dictionaries
创立字典

1 def run():2     knights = {"Arthur": "king", "Lancelot": "brave", "Galahad": "pure", "Robin": "not-quite-so-brave"}3     return knights4 5 #This is just for you to see what happens when the function is called6 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 knights5 6 #This is just for you to see what happens when the function is called7 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 here4     del knights["Galahad"]5     return knights6 7 #This is just for you to see what happens when the function is called8 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 keysToDel20      for key in keysToDel:21          del family[key]22      return family23 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 here3     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])

?

文章结束给大家分享下程序员的一些笑话语录: 腾讯的动作好快,2010年3月5日19时28分58秒,QQ同时在线人数1亿!刚刚看到编辑发布的文章,相差才2分钟,然后连专题页面都做出来了,他们早就预料到了吧?(其实,每人赠送10Q币,轻轻松松上两亿!)

热点排行