Riju21

13_minified_loop

Mar 27th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. from collections import Counter
  2.  
  3. x = [1,2,3,4,5,6]
  4.  
  5. new_x = [i for i in x]  # instead for traditional for loop
  6. print(new_x)
  7.  
  8. new_x1 = [i * i for i in x] # square of elements
  9. print(new_x1)  
  10.  
  11. new_x2 = [i for i in x if i % 2 == 0]   # total even
  12. print(new_x2)
  13.  
  14. new_x3 = [(i, j) for i in x for j in 'abcd']  # all pair
  15. print(new_x3)
  16.  
  17. # dictionary comprehensions
  18. # ---------------------------------
  19.  
  20. # for lang,frame in zip( language,frameWork) :
  21. #     combine[lang] = frame
  22. # print(combine)
  23.  
  24. language = ['js', 'php', 'python']
  25. frameWork = ['vetur', 'cakephp', 'flask']
  26. combine = {lang: frame for lang, frame in zip(language, frameWork)}
  27. print(combine)  
  28.  
  29. # set comprehensions
  30. # ---------------------------------
  31.  
  32. y = [1, 2, 3, 3, 4, 547, 9, 9, -1, 2, 3, 8, 5, 6, 1]
  33.  
  34. # new_y = set()
  35. # for i in y:
  36. #     new_y.add(i)
  37. # print(new_y)
  38.  
  39. # or,
  40.  
  41. new_y = {i for i in y}    # get unique number
  42. print(new_y)
  43.  
  44. repeated_numbers = dict(Counter(y))  # all repeated number
  45. print(repeated_numbers)
Advertisement
Add Comment
Please, Sign In to add comment