task = []
for i in range(10):
task.append(i**2)
#list
task = [i**2 for i in range(10)]
#map印射
task = list(map(lambda x: x**2, range(10)))
# map (None,[1,2,3],[4,5,6],[7,8,9,10]
#filter筛选
task = filter(lambda x:1<x<10,[-2,-1,0,1,2])
#reduce
from functools import reduce
task = reduce(lambda x, y: x+y, [1,2,3,4,5])
#分析从外到内,化繁为简
#示例2
task = []
for i in range(10):
season = i #>>>>>>>>>>>>>>>>>>>>>>>>
for j in range(5):
team = j
task.append((j, i, season, team))
print(task)
def func(i):
# season = i # >>>>>>>>>>>>>>>>>>>>>>>>>>>func2内不存在i
# return list(map(lambda x: func2(i,x), range(5)))
return [func2(i,j) for j in range(5)]
def func2(i, j): # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>实际返回两个变量
team = j
season = i
return j, i, season, team
task = [func(i) for i in range(10)]
print(sum(task,[]))
# 使用sum
list_1 = [[1, 2], [3, 4, 5], [6, 7], [8], [9]]
list_2 = sum(list_1, [])
print(list_2)
# 只可以使用递归(这个有不有办法改写为Pythonic???)
def flat(nums):
res = []
for i in nums:
if isinstance(i, list):
res.extend(flat(i))
else:
res.append(i)
return res
print(flat(list_1))
# 示例3
list_1 = [1,2,3,4,5,6,7,8,9,0]
list_2 = [4,6,7,9,10]
print(list(filter(lambda w: w in list_1, list_2)))
|