设为首页 收藏本站
查看: 751|回复: 0

[经验分享] 输出集合《Python for Beginners》学习笔记(6) 输出集合

[复制链接]

尚未签到

发表于 2017-5-6 12:18:13 | 显示全部楼层 |阅读模式
  在改章节中,我们重要介绍输出集合的容内,自我感觉有个不错的建议和大家分享下
  《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])

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373802-1-1.html 上篇帖子: python 文件的读取、创建、追加、删除、清空 下篇帖子: python Tkinter学习笔记 基础UI控件 01
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表