map高級用法?
1.當list是一維的
def mul_Ten(n):return n*10
l1 = [1, 2, 3, 4, 5, 6, 7, 8]
l2 = map(mul_Ten, l1)
print(l2)
for item in l2:
print(item)
2.當list包含多維l1 = [1, 2, 3, [4, 5]]l2 = map(mul_Ten, l1)
print(l2)
for item in l2:
print(item)
從結(jié)果上看,map是并行的,它將對象作為參數(shù)傳遞進去,而在python中l(wèi)ist的乘就是重復n次,得到這個結(jié)果。3.實際需求l1 = list(map(int, input().split()))
l1.sort()
print(l1)