Python轻量级循环-列表推导式

AI入门学习

共 1776字,需浏览 4分钟

 ·

2021-01-23 13:57

'+j for i in girls for j in boys if i[0]==j[0]]"],[20,"\n","24:\"0WUn\"|36:131"],[20,"['alice<->arnold', 'bernice<->bob', 'bernice<->bernod', 'clarice<->chris']"],[20,"\n","24:\"LIYq\"|36:131"],[20,"\n","24:\"nuKS\"|32:1"],[20,"多循环+多条件"],[20,"\n","24:\"JNiT\"|32:1"],[20,"第一个字母+第二个字母均相同"],[20,"\n","24:\"AnHr\""],[20,"[i+'<->'+j for i in girls for j in boys if (i[0]==j[0] and i[1]==j[1])]"],[20,"\n","24:\"8YIq\"|36:131"],[20," ['bernice<->bernod']"],[20,"\n","24:\"Wz6V\"|36:131"],[20,"\n","24:\"eXEM\"|32:1"],[20,"元组循环"],[20,"\n","24:\"mumJ\"|32:1"],[20,"列表可以直接循环,改成元组也是可以的,但是直接生产了迭代器"],[20,"\n","24:\"CjHw\""],[20,"(i+'<->'+j for i in girls for j in boys if i[0]==j[0])"],[20,"\n","24:\"ibSI\"|36:131"],[20,"at 0x0000015413B781C8>"],[20,"\n","24:\"vJU3\"|36:131"],[20,"\n","24:\"bneG\"|36:131"],[20,"tuples = (i+'<->'+j for i in girls for j in boys if i[0]==j[0])"],[20,"\n","24:\"7SCB\"|36:131"],[20,"for t in tuples:"],[20,"\n","24:\"cPWb\"|36:131"],[20,"    print(t)"],[20,"\n","24:\"t6o6\"|36:131"],[20,"alice<->arnold"],[20,"\n","24:\"mmCv\"|36:131"],[20,"bernice<->bob"],[20,"\n","24:\"DSuE\"|36:131"],[20,"bernice<->bernod"],[20,"\n","24:\"j7LD\"|36:131"],[20,"clarice<->chris"],[20,"\n","24:\"pBR7\"|36:131"],[20,"\n","24:\"81km\"|32:1"],[20,"效率问题"],[20,"\n","24:\"sOEF\"|32:1"],[20,"男孩-女孩名字对的例子中,其实效率不是很高,因为程序会检测每个可能的配对。Python中有很多解决这个问题的方法,下面是其中一种比较高效的方法。"],[20,"\n","24:\"Xv2b\""],[20,"letterGirls = {}"],[20,"\n","24:\"7dmT\"|36:131"],[20,"for girl in girls:"],[20,"\n","24:\"ST3E\"|36:131"],[20,"    letterGirls.setdefault(girl[0],[]).append(girl)"],[20,"\n","24:\"zzB6\"|36:131"],[20,"print([i+'<->'+j for j in boys  for i in letterGirls[j[0]]])"],[20,"\n","24:\"sn8L\"|36:131"],[20,"['clarice<->chris', 'alice<->arnold', 'bernice<->bob', 'bernice<->bernod']"],[20,"\n","24:\"5qCv\"|36:131"],[20,"看看我们构建的字典长啥样"],[20,"\n","24:\"t6WC\""],[20,"letterGirls"],[20,"\n","24:\"HSNh\"|36:131"],[20,"{'a': ['alice'], 'b': ['bernice'], 'c': ['clarice']}"],[20,"\n","24:\"v1e3\"|36:131"],[20,"注意:","8:1"],[20,"setdefault()方法,是字典的一个方法,类似于get()方法,如果键不存在于字典中,将会添加键并将值设为默认值。如果存在,则返回该字段的值。","0:\"%23000000\"|31:2"],[20,"语法:dict.setdefault(key, default=None)"],[20,"\n","24:\"INBu\"|linespacing:\"115\""]]">

概  述

Python 列表推导式(list comprehension)利用其他列表创建新的列表,工作方式类似于for循环,使用列表推导式可以快速生成一个列表,或者根据某个列表生成满足指定需求的列表。


单循环

[i for i in range(10)][0123456789]


单循环+条件

0-20中所有能被3整除的数是多少

[i for i in range(20) if i%3==0][0, 3, 6, 9, 12, 15, 18]

 

多循环-2次

'ABC'和'EGF'所有可能的两两组合

[i+j for i in 'ABC' for j in 'EFG']['AE''AF''AG''BE''BF''BG''CE''CF''CG']


多循环-3次

'ABC'、'EFG'、‘HIJ’所有可能的三三组合

[i+j+u for i in 'ABC' for j in 'EFG' for u in 'HIJ']['AEH', 'AEI', 'AEJ', 'AFH', 'AFI', 'AFJ', 'AGH', 'AGI', 'AGJ', 'BEH', 'BEI', 'BEJ', 'BFH', 'BFI', 'BFJ', 'BGH', 'BGI', 'BGJ', 'CEH''CEI''CEJ''CFH''CFI''CFJ''CGH''CGI''CGJ']



多循环+单条件

首字母相同的男孩 女孩组合

girls = ['alice','bernice','clarice']boys  = ['chris','arnold','bob','bernod'][i+'<->'+j for i in girls for j in boys if i[0]==j[0]]['alice<->arnold', 'bernice<->bob', 'bernice<->bernod', 'clarice<->chris']

 

多循环+多条件

第一个字母+第二个字母均相同

[i+'<->'+j for i in girls for j in boys if (i[0]==j[0] and i[1]==j[1])] ['bernice<->bernod']



元组循环

列表可以直接循环,改成元组也是可以的,但是直接生产了迭代器

(i+'<->'+j for i in girls for j in boys if i[0]==j[0]) at 0x0000015413B781C8>
tuples = (i+'<->'+j for i in girls for j in boys if i[0]==j[0])for t in tuples: print(t)alice<->arnoldbernice<->bobbernice<->bernodclarice<->chris



效率问题

男孩-女孩名字对的例子中,其实效率不是很高,因为程序会检测每个可能的配对。Python中有很多解决这个问题的方法,下面是其中一种比较高效的方法。

letterGirls = {}for girl in girls:    letterGirls.setdefault(girl[0],[]).append(girl)print([i+'<->'+j for j in boys  for i in letterGirls[j[0]]])['clarice<->chris', 'alice<->arnold', 'bernice<->bob', 'bernice<->bernod']

看看我们构建的字典长啥样

letterGirls{'a': ['alice'], 'b': ['bernice'], 'c': ['clarice']}

注意:setdefault()方法,是字典的一个方法,类似于get()方法,如果键不存在于字典中,将会添加键并将值设为默认值。如果存在,则返回该字段的值。语法:dict.setdefault(key, default=None)



学习交流群

发现“在看”和“赞”了吗,戳我试试吧
浏览 35
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报