购买主题
沙发{:10_327:}
列表推导式的高级用法:
words
['Great', 'FishC', 'Brilliant', 'Excellent', 'Fantanstic']
>>> fwords =[ w for w in words if w == "F"]
>>> fwords
['FishC', 'Fantanstic']
>>> matrix =[;
SyntaxError: invalid syntax
>>> matrix =[,
,
]
>>> flatten =
>>> flatten
>>> flatten =[]
>>> for row in matrix:
for col in row:
flatten.append(col)
>>> flatten
>>> [ x + y for x in "FishC" for y in "FishC"]
['FF', 'Fi', 'Fs', 'Fh', 'FC', 'iF', 'ii', 'is', 'ih', 'iC', 'sF', 'si', 'ss', 'sh', 'sC', 'hF', 'hi', 'hs', 'hh', 'hC', 'CF', 'Ci', 'Cs', 'Ch', 'CC']
>>> [ x + y for x in "fishc" for y in "FISHC"]
['fF', 'fI', 'fS', 'fH', 'fC', 'iF', 'iI', 'iS', 'iH', 'iC', 'sF', 'sI', 'sS', 'sH', 'sC', 'hF', 'hI', 'hS', 'hH', 'hC', 'cF', 'cI', 'cS', 'cH', 'cC']
>>> _ =[]
>>> for x in "fishc":
for y in "FISHC":
_.append(x+y)
>>> _
['fF', 'fI', 'fS', 'fH', 'fC', 'iF', 'iI', 'iS', 'iH', 'iC', 'sF', 'sI', 'sS', 'sH', 'sC', 'hF', 'hI', 'hS', 'hH', 'hC', 'cF', 'cI', 'cS', 'cH', 'cC']
>>> [ for x in range(10) if x %2 == 0 for y in range(10) if y %3 == 0]
[, , , , , , , , , , , , , , , , , , , ]
>>> _ = []
>>> for x in range(10):
if x%2 == 0:
for y in range(10):
if y %3 == 0:
_.append()
>>> _
[, , , , , , , , , , , , , , , , , , , ]
>>>
**1.再谈列表推导式**
B不是一个二维列表,只是对同一个列表的三次引用
>>> B = [ * 3] * 3
>>> B
[, , ]
>>> B = 1
>>> B
[, , ]
创建二维列表A
>>> A = * 3
>>> for i in range(3):
A = * 3
>>> A
[, , ]
>>> A = 1
>>> A
[, , ]
用列表推导式创建嵌套列表
>>> S = [ * 3 for i in range(3)]
>>> S
[, , ]
>>> S = 1
>>> S
[, , ]
列表推导式还可以添加一个用于筛选的if分句
>>> even =
>>> even
先执行for语句,在执行if语句,最后执行左侧的表达式
>>> even =
>>> even
从这五个单词中筛选出‘F’开头的单词
>>> words = ["Great", "FishC", "Brilliant", "Excellent", "Fantistic"]
>>> words = ["Great", "FishC", "Brilliant", "Excellent", "Fantastic"]
>>> fwords = == 'F']
>>> fwords
['FishC', 'Fantastic']
列表推导式还可以变得更复杂一些,那就是实现嵌套
[expression for target1 in iterable1
for target2 in iterable2
...
for targetN in iterableN]
将二维列表降级为一维列表
>>> matrix = [, , ]
>>> flatten =
>>> flatten
转换成循环的实现
>>> flatten = []
>>> for row in matrix:
for col in row:
flatten.append(col)
>>> flatten
笛卡尔乘积
>>>
['fF', 'fI', 'fS', 'fH', 'fC', 'iF', 'iI', 'iS', 'iH', 'iC', 'sF', 'sI', 'sS', 'sH', 'sC', 'hF', 'hI', 'hS', 'hH', 'hC', 'cF', 'cI', 'cS', 'cH', 'cC']
转换成循环
>>> _ = []
>>> for x in "fishc":
for y in "FISHC":
_.append(x + y)
>>> _
['fF', 'fI', 'fS', 'fH', 'fC', 'iF', 'iI', 'iS', 'iH', 'iC', 'sF', 'sI', 'sS', 'sH', 'sC', 'hF', 'hI', 'hS', 'hH', 'hC', 'cF', 'cI', 'cS', 'cH', 'cC']
[某个变量名是临时的或者无关紧要的,就可以直接用一个下划线当变量名]
列表推导式的终极用法
[expression for target1 in iterable1 if condition1
for target2 in iterable2 if condition2
...
for targetN in iterableN if conditionN]
举例
>>> [ for x in range(10) if x % 2 == 0 for y in range(10) if y % 3 == 0]
[, , , , , , , , , , , , , , , , , , , ]
转换成循环
>>> _ = []
>>> for x in range(10):
if x % 2 == 0:
for y in range(10):
if y % 3 == 0:
_.append()
>>> _
[, , , , , , , , , , , , , , , , , , , ]
程序设计原则“KISS”(全称:Keep It Simple & Stupid)
{:10_265:}
嵌套实现更多{:10_335:}
打卡025
已复习,谢谢
阵亡
加油
混鱼
#一、用列表推导式创建二维列表
list_ = [*3 for x in range(3)]
print(list_)
#二、找到首字母为F的单词
words = ["FishC","Billon","Fatastic"]
fwords = == 'F']
print(fwords)
#三、笛卡尔乘积
#临时变量用_/__命名
#列表推导式法
_ =
print(_)
#循环法
__= []
for x in "FishC":
for y in "fishc":
__.append(x+y)
print(__)
#四、找出[0,10)以内是2的倍数的数和是2的倍数的数的所有排列组合
#列表推导式
_ = [ for x in range(10) if x % 2 == 0 for y in range(10) if y % 3 == 0]
print(_)
#循环法
__ = []
for x in range(10):
if x % 2 == 0:
for y in range(10):
if y % 3 == 0:
__.append()
print(__)
[, , ]
['FishC', 'Fatastic']
['Ff', 'Fi', 'Fs', 'Fh', 'Fc', 'if', 'ii', 'is', 'ih', 'ic', 'sf', 'si', 'ss', 'sh', 'sc', 'hf', 'hi', 'hs', 'hh', 'hc', 'Cf', 'Ci', 'Cs', 'Ch', 'Cc']
['Ff', 'Fi', 'Fs', 'Fh', 'Fc', 'if', 'ii', 'is', 'ih', 'ic', 'sf', 'si', 'ss', 'sh', 'sc', 'hf', 'hi', 'hs', 'hh', 'hc', 'Cf', 'Ci', 'Cs', 'Ch', 'Cc']
[, , , , , , , , , , , , , , , , , , , ]
[, , , , , , , , , , , , , , , , , , , ]
>>>
打卡~~{:10_254:}{:10_254:}
学到了+
打卡
打卡完结{:10_256:}
KEEP IT SIMPLE AND STUPID !
要加油啊!
打卡{:10_257:}
谢谢
打卡记录学习
已有 25 人购买 本主题需向作者支付 5 鱼币 才能浏览