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

[i for i in range(10)][0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

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

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

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

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

'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])<generator object <genexpr> 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

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']}

qq群704220115