2021年2月5日星期五

Updating the range of list in function argument

Problem to solve: Define a Python function remdup(l) that takes a non-empty list of integers l and removes all duplicates in l, keeping only the last occurrence of each number. For instance: if we pass this argument then remdup([3,1,3,5]) it should give us a result [1,3,5]

def remdup(l):      for last in reversed(l):          pos=l.index(last)          for search in reversed(l[pos]):              if search==last:                  l.remove(search)        print(l)      remdup([3,5,7,5,3,7,10])    # intended output [5, 3, 7, 10]  

On line 4 for loop I want the reverse function to check for each number excluding index[last] but if I use the way I did in the above code it takes the value at pos, not the index number. How can I solve this

https://stackoverflow.com/questions/66072955/updating-the-range-of-list-in-function-argument February 06, 2021 at 10:14AM

没有评论:

发表评论