Python list of several processing method

  • map function usage:
ss = map(str,range(3))
# ['1','2','3']
ii = map(int,ss)
# [1,2,3]
  • lambda functions usage:
numbers = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
print(sum(map(lambda x: x+10, numbers)))
  • list usage cycle
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]
  • Comprehensive use:
    TXT file is loaded:
    TXT file schematically:
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)]
Published 36 original articles · won praise 0 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_38102912/article/details/85267576