简体   繁体   中英

Comparing nested lists

I have two lists, one of the form:

[a1, b1, [[c1, d1, e1], [f1, g1, h1], etc, etc], etc]

and the other, a dictionary, whose entries are in the form:

[[a2, b2, c2], [d2, e2, f2], etc, etc] .

I need to compare the first entries of those two sub lists there and find any which are the same, and any in the first which don't appear at all in the second.

Foe example, if c1 = d2 , I'd want to know, and if f1 isn't equal to either a2 or d2 , I'd want to know that.

Anyway, I'm having a bit of trouble implementing this properly, any help would be appreciated.

Thanks!

(I'm not sure how clear the list formats are to understand, sorry if they're still confusing)

CODE SAMPLE:

for row in range(0, len(command[2])):

    counter = 0

    for nodeRows in range(0, len(nodeTable[command[0]])):

        if nodeTable[command[0]][nodeRows][0] == command[2][row][0]:

            if ((command[2][row][2]) + 1) < nodeTable[command[0]][nodeRows][2]:

                counter += 1

                newrow = command[2][row]
                newrow[1] = command[1]
                newrow[2] = newrow[2] + 1

                nodeTable[command[0]][nodeRows] = newrow

                change = 'true'

I imagine this doesn't help. The code is a bit monolithic (that's why I didn't post it initially). But I'm basically trying to compare two values. The first values of the items from the list in the 3rd position of another list and the first values of the items from the lists contained in another list.

Um...sorry. I have tried making the code simpler, but it's a bit complicated.

I'm not sure I understand correctly your problem but I'll give it a try. I guess you need to compare only the first element of every sublist of 3 elements.

So first I separate all the first elements, then make the comprarisson.

Here is the code with some doctest so you can check if it does what you are asking:

def compare(l0, l1):
    """
    >>> l0 = [1, 2, [[10, 20, 30], [40, 50, 60], [70, 80, 90]], 3]
    >>> l1 = [[11, 21, 31], [41, 51, 61], [71, 81, 91]]
    >>> compare(l0, l1)
    ([], [10, 40, 70])

    >>> l0 = [1, 2, [[10, 20, 30], [40, 50, 60], [70, 80, 90]], 3]
    >>> l1 = [[10, 21, 31], [41, 51, 61], [71, 81, 91]]
    >>> compare(l0, l1)
    ([10], [40, 70])

    >>> l0 = [1, 2, [[10, 20, 30], [40, 50, 60], [70, 80, 90]], 3]
    >>> l1 = [[10, 21, 31], [40, 51, 61], [70, 81, 91]]
    >>> compare(l0, l1)
    ([10, 40, 70], [])
    """
    first_entries_l0 = [x[0] for x in l0[2]]
    first_entries_l1 = [x[0] for x in l1]

    equals = [x for x in first_entries_l0 if x in first_entries_l1]
    unique = [x for x in first_entries_l0 if x not in first_entries_l1]

    return equals, unique

To test the code just copy it to a file 'code.py' and run it with:

python -m doctest code.py

You could make it more efficient using sets and looping only once but I'm not even sure this solves your problem so I'll leave that to you.

The answer is: transform your current datastructure to a proper one. Presumably the inputs are be defined by yourself, so you should not write better code to deal with ugly structures, but improve the structures. If you are writing against a bad API, map the API to a useful structure.

You will have to post the whole code to get a proper answer, because the problem is in the definitions. I guess you will have to refactor the whole module and start again, because this is simply bad code.

Some ideas: could command be a tree? a queued list? a matrix? a class? why does the length of the items vary, and why do you want to compare different subitems? Try using classes and override __cmp__ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM