楼主: ReneeBK
3191 31

Powerful Python, 2nd Edition [推广有奖]

11
ReneeBK 发表于 2017-8-21 09:59:28
  1. Multiple Sources and Filters
  2. You can have several for VAR in SEQUENCE clauses. This lets you
  3. construct lists based on pairs, triplets, etc., from two or more source
  4. sequences:
  5. >>> colors = ["orange", "purple", "pink"]
  6. >>> toys = ["bike", "basketball", "skateboard", "doll"]
  7. >>>
  8. >>> [ color + " " + toy
  9. ... for color in colors
  10. ... for toy in toys ]
  11. ['orange bike', 'orange basketball', 'orange skateboard',
  12. 'orange doll', 'purple bike', 'purple basketball',
  13. 'purple skateboard', 'purple doll', 'pink bike',
  14. 'pink basketball', 'pink skateboard', 'pink doll']
复制代码

12
ReneeBK 发表于 2017-8-21 10:02:07
  1. >>> # User is a class with "email" and "is_active" fields.
  2. ... # all_users is a list of User objects.
  3. >>> # Sorted list of active user's email addresses.
  4. ... # Passing in a generator expression.
  5. >>> sorted((user.email for user in all_users
  6. ... if user.is_active))
  7. ['fred@a.com', 'sandy@f.net', 'tim@d.com']
  8. >>>
  9. >>> # Omitting the inner parentheses.
  10. ... # Still passing in a generator expression!
  11. >>> sorted(user.email for user in all_users
  12. ... if user.is_active)
  13. ['fred@a.com', 'sandy@f.net', 'tim@d.com']
复制代码

13
小美人鱼6 发表于 2017-8-21 13:36:58
多谢分享

14
iambic 发表于 2017-8-21 17:22:52
thank u

15
seoulcityyxx 在职认证  发表于 2017-8-22 15:28:08
过来看一看

16
stupidpinggu 发表于 2017-8-22 16:39:35
谢谢分享,不过楼主你的链接失效了

17
xialiangjsxz 发表于 2017-8-22 22:56:51
thank for sharing it

18
kenxwong 发表于 2017-8-23 13:35:23
谢谢分享

19
iid_garch 发表于 2017-8-24 10:26:43

20
mxnmxx 发表于 2017-8-25 13:10:45
多谢分享

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-22 01:12