Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. def rmNegatives(L):
  2. subscript = 0
  3. for num in L:
  4. if num < 0:
  5. L = L[:subscript] + L[subscript:]
  6. subscript += 1
  7. return L
  8.  
  9. new_list = [i for i in old_list if i>=0]
  10.  
  11. >>> old_list = [1,4,-2,94,-12,-1,234]
  12. >>> new_list = [i for i in old_list if i>=0]
  13. >>> print new_list
  14. [1,4,94,234]
  15.  
  16. def rmNegatives(L):
  17. i = 0
  18. while i < len(L):
  19. if L[i]<0:
  20. del L[i]
  21. else:
  22. i+=1
  23. return L
  24.  
  25. L = filter(lambda x: x > 0, L)
  26.  
  27. L = L[:subscript] + L[subscript:]
  28.  
  29. >>> l = [1,2,3,4]
  30. >>> l[:2] + l[2:]
  31. [1, 2, 3, 4]
  32.  
  33. def rmNegatives(L):
  34. subscript = 0
  35. for num in L: # here you run over a list which you mutate
  36. if num < 0:
  37. L = L[:subscript] + L[subscript:] # here you do not change the list (see comment above)
  38. subscript += 1 # you have to do this only in the case, when you did not remove an element from the list
  39. return L
  40.  
  41. def rmNegatives(L):
  42. subscript = 0
  43. for num in list(L):
  44. if num < 0:
  45. L = L[:subscript] + L[subscript+1:]
  46. else:
  47. subscript += 1
  48. return L
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement