Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def rmNegatives(L):
- subscript = 0
- for num in L:
- if num < 0:
- L = L[:subscript] + L[subscript:]
- subscript += 1
- return L
- new_list = [i for i in old_list if i>=0]
- >>> old_list = [1,4,-2,94,-12,-1,234]
- >>> new_list = [i for i in old_list if i>=0]
- >>> print new_list
- [1,4,94,234]
- def rmNegatives(L):
- i = 0
- while i < len(L):
- if L[i]<0:
- del L[i]
- else:
- i+=1
- return L
- L = filter(lambda x: x > 0, L)
- L = L[:subscript] + L[subscript:]
- >>> l = [1,2,3,4]
- >>> l[:2] + l[2:]
- [1, 2, 3, 4]
- def rmNegatives(L):
- subscript = 0
- for num in L: # here you run over a list which you mutate
- if num < 0:
- L = L[:subscript] + L[subscript:] # here you do not change the list (see comment above)
- subscript += 1 # you have to do this only in the case, when you did not remove an element from the list
- return L
- def rmNegatives(L):
- subscript = 0
- for num in list(L):
- if num < 0:
- L = L[:subscript] + L[subscript+1:]
- else:
- subscript += 1
- return L
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement