Advertisement
Luninariel

Mapping Examples

Sep 25th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. #Map to add 1 to each value in a list
  2. ls = [34, 6, 8, 2, 1, 5]
  3. lst =['bobbby     ', '     tim','  Susan  ']
  4. list(map(lambda x: x + 1, ls))
  5.  
  6. #Map to make all numbers in a list positive
  7. list(map(lambda x: abs(x), ls))
  8.  
  9. #or
  10. list(map(lambda x: -x if x < 0 else x, ls))
  11.  
  12. #Map to remove whitespace from all strings in a list
  13. list(map(lambda x: x.strip(),lst))
  14.  
  15. #Map to transform a dictionary into Key-Value pairs
  16. d = {4:'Frank', 9:'Ada', 32:'Sue'}
  17. list(map(lambda k: (k, d[k]), d))
  18.  
  19. #Reduce function
  20. def mycount(i):
  21.     count = 0
  22.     for x in i:
  23.         count += 1
  24.     return count
  25. #mycount(range(4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement