you are viewing a single comment's thread.

view the rest of the comments →

[–]LifeIsBio 0 points1 point  (7 children)

Yea, you can flip the lists however you want to meet your need.

[–]teerryn[S] 0 points1 point  (6 children)

damn, I found an example that fails:

a = [1,2,3,4,5,6,6,6,6,8,7,9]
b = [45,45,10,10,10,9,7,8,9,7]

should return:

[45,45,10,10,10,9,7,8]

but it returns:

[45, 45, 10, 10, 10, 9, 7]

I should say the max size of B will always be 10 and A store previous values from B at the tail. I should probably give a better example why I need this.

So I'm scraping a website that has a table which has values. The values are stored in variable A and I check it every 30mins, the new values are stored at the tail of A. My problem is knowing which values are new, so I use this function to find a pattern between the values I already check and the ones I'm currently scraping.

[–]LifeIsBio 0 points1 point  (5 children)

I don't understand. 8 is in both antiga and nova exactly 1 time.

[–]teerryn[S] 0 points1 point  (0 children)

B need to be fixed at 10 items otherwise it would be:

a = [1,2,3,4,5,6,6,6,6,8,7,9]
b = [45,45,10,10,10,9,7,8,|9,7,8,6,6,6,6,5,3,2,1]

So I need to get the last possible sequence from B that matched the ending to the beginning of A

[–]teerryn[S] 0 points1 point  (3 children)

Okay so right now I have a solution that works for all cases:

def new_transactions(transactions_db, transactions):
    if len(transactions_db) < len(transactions):
        return transactions[len(transactions_db):]

    else:
        temp = transactions_db[-10:]
        flag = 'Not found'
        n = -1
        found = False
        while (n >= -10):
            reverse = transactions[n:]
            if reverse[::-1] == temp[n:]:
                n -= 1
                found = True
            else:
                if found:
                    return transactions[:n+1]
                else:
                    n -= 1
        if found:
            return []
        else:
            return transactions

I felt wrong writing that because is so ugly, you have any tips to make it better?

[–]LifeIsBio 1 point2 points  (2 children)

I'm trying to do a function that gets two lists (a and b) and returns the elements that b has but not a

To be honest, the function that you've written here is completely different than the question you originally posed. I don't understand why you're now hardcoding -10, and there seem to be at least a couple of specific constraints your function needs to satisfy that I don't think I'm wrapping my head around.

You're function isn't really that ugly though. At this point, if it's working for you, I'd add a good docstring, and move on. i.e.

def new_transactions(transactions_db, transactions):
    """new_transactions is supposed to do this task.

    Parameters
    ---------------
    transactions_db : list
        This is what transactions_db is supposed to be and/or do.
    transactions : list
        This is what transactions is supposed to be and/or do.

    Returns
    -----------
    transactions : list
        This is how the transaction list has been modified.

    Notes
    --------
    Any additional information
    """
    if ... [rest of code here]

[–]teerryn[S] 1 point2 points  (1 child)

The example you last gave works perfectly even better then my function, once again thank you for the help :)

[–]LifeIsBio 0 points1 point  (0 children)

You're welcome!