- map 函数用法:
ss = map(str,range(3))
# ['1','2','3']
ii = map(int,ss)
# [1,2,3]
- lambda 函数用法:
numbers = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
print(sum(map(lambda x: x+10, numbers)))
- list 循环用法
numbers = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
[x for x in numbers if x>100]
# [101, 108, 108, 111, 119, 111, 114, 108]
- 综合使用:
txt文件载入:
txt 文件示意:
1 2 32
1 3 46
1 4 50
1 5 57
1 6 57
1 7 32
1 8 51
code:
x= [ map(lambda x: int(x), x.split(' ')) for x in open('1.txt', 'r').read().split('\n')[1:-1]]
# x:
#[(1, 2, 32), (1, 3, 46), (1, 4, 50), (1, 5, 57), (1, 6, 57), (1, 7, 32), (1, 8, 51)]