Advertisement
furas

Python - compare three version

Apr 5th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. list1 = [2, 3, 5, 1, 10, -2, 5]
  2.  
  3. # ---- oryginal ---
  4.  
  5. list2 = []
  6.  
  7. iterations = len(list1) - 1
  8. position = 1
  9. for i in range(0, iterations):
  10.     denominator = (list1[position])
  11.     list2.append(list1[i] / denominator)
  12.     position = position + 1
  13.  
  14. print(list2)
  15.  
  16. # ---- with all modifications but still with range(len()) ---
  17.  
  18. list2 = []
  19.  
  20. for i in range(len(list1)-1):
  21.     list2.append(list1[i] / list1[i+1])
  22.  
  23. print(list2)
  24.  
  25. # ---- without range(len()) ---
  26.  
  27. list2 = []
  28.  
  29. for a, b in zip(list1, list1[1:]):
  30.     list2.append(a/b)
  31.  
  32. print(list2)
  33.  
  34. # --- as list comprehension ---
  35.  
  36. list2 = [a/b for a, b in zip(list1, list1[1:])]
  37.  
  38. print(list2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement