Dynamic substrings in the list. 10 elements before variable

I have a problem with dynamic substrings. I have a list that can contain 1000 items, 100 items, or even 20. I want to make a copy of this list that has items between -10 and a variable. For example (pseudocode):

L = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
variable = 12
print L[substring:variable]
>>> L = [2,3,4,5,6,7,8,9,10,12]

      

I cannot figure out how to make it correct. The point is that a variable always changes by one.

Here is my piece of code:

def Existing(self, Pages):
     if(self.iter <= 10):
         list = self.other_list[:self.iter]
     else:
         list = self.other_list[self.iter-10:self.iter]
     result = 0
     page = Pages[0]

     list.reverse()

     for blocks in Pages:
         if(list.index(blocks) > result):
             result = list.index(blocks)
             page = blocks

     return page

      

This method searches for the element with the highest index. This part may be unclear. Suppose we have

list = [1,2,3,4,1,5,2,1,2,3,4]

      

The method should return 5 because this is the best element. The list has duplicates, and .index () returns the index of the first element, so I reverse the list. With this code, sometimes the program returns that some element does not exist in the List. The problem (after a deep review with debbuger) is related to substrings in self.other_list.

Could you please help me with this problem? How to do it right? Thanks for any advice.

EDIT: Because my problem is not clear enough (I was sure it might be), so here are examples.

Ok, so the Pages list is a list that uses the current pages. The second list "list" is a list of all pages that have been used. The method looks for pages that are already in use and select the one that has not been used for the longest time. With the word "use" I mean the index of the element. What does the largest element mean? The one with the smallest index (remember duplicates, last duplicates means the real index).

So we have:

Pages = [1,3,5,9]

      

and

list = [1,2,5,3,6,3,5,1,2,9,3,2]

      

The method should return 5.

To summarize: I am looking for a substring that gives the result:

With list =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] 
For variable 12: [2,3,4,5,6,7,8,9,10,12] 
for 13: [3,4,5,6,7,8,9,10,11,13] 

      

ect :-) I know the problem can be tricky. So I would suggest that you focus on substrings only. :-) Thank you very much!

+3


source to share


1 answer


If I understood your problem correctly, you want to find the index of the elements from the pages that are at the minimum position in lst

(including duplicates).

So, to do this, you need to flip the list first and then first the index of each element in the pages in lst if the element is not found, then return negative infinity. From these indices, you can find the maximum element and you will get the answer.

from functools import partial

pages = [1,3,5,9]
lst = [1,2,5,3,6,3,5,1,2,9,3,2]

def get_index(seq, i):
    try:
        return seq.index(i)
    except ValueError:
        return float('-inf')


lst.reverse()
print max(pages, key=partial(get_index, lst))
#5

      




Note that the above method will take quadratic time, so it won't work well for huge lists. If you are not interested in some extra memory but linear time, you can use set and dict to do this:

pages_set = set(pages)
d = {}
for i, k in enumerate(reversed(lst), 1):
    if k not in d and k in pages_set:
        d[k] = len(lst) - i

print min(d, key=d.get)
#5

      

+2


source







All Articles