Skip to content Skip to sidebar Skip to footer

Comparing List With A List Of Lists

I have a list string_array = ['1', '2', '3', '4', '5', '6'] and a list of lists multi_list = [['1', '2'], ['2', '3'], ['2', '4'], ['4', '5'], ['5', '6']] The first element of each

Solution 1:

Here's a few concepts that will help you, but I'm not going to give you the complete solution. You should do so on your own to sink in the concepts.

To make an empty dictionary of lists

{
    '1': [],
    '2': [],
    '3': [],
    '4': [],
    '5': [],
    '6': [],
}

you can use a for loop:

list_one = ['1', '2', '3', '4', '5', '6']

my_dict = {}
forvaluein list_one:
    my_dict[value] = []

You could also get fancy and use a dictionary comprehension:

my_dict = {value: [] for value in list_one}

Now you'll need to loop over the second list and append to the current list. e.g. to append to a list you can do so in a few ways:

a = [1,2]
b = [3,4]
c = 5

# add a list to a list
a += b
# now a = [1,2,3,4]# add a list to a list
b.append(c)
# now b = [3,4,5]

To chop up lists you can use slice notation:

a = [1,2,3,4]
b = a[:2]
c = a[2:]
# now b = [1,2], and c = [3,4]

And to access an item in a dictionary, you can do so like this:

a = { '1': [1,2,3] }
a['1'] += [4,5,6]
# now a = { '1': [1,2,3,4,5,6] }

Solution 2:

flat = ['1', '2', '3', '4', '5', '6']
nested = [['1', '2'], ['2', '3'], ['2', '4'], ['4', '5'], ['5', '6']]

result = {key: [] forkeyin flat}

forkeyin result:
    result[key] = [nest[1] fornestin nested if key == nest[0]]


#{'1': ['2'], '2': ['3', '4'], '3': [], '4': ['5'], '5': ['6'], '6': []}

Using your flat list elements as keys, you can generate a dictionary and use empty lists are the values. Empty strings or None would also work if using list comprehension to generate the values later.

Then just iterate over the keys in the dictionary and your nested list to append the results to the appropriate keys.

Furthermore;

You can reduce the above down to a single line outputting the same result.

result = {key: [nest[1] for nest in nested if key == nest[0]] for key in flat}

Output:

{'1': ['2'], '2': ['3', '4'], '3': [], '4': ['5'], '5': ['6'], '6': []}

EDIT:

After assessing Juanpa's comment and testing the efficiency of the above code, it is clear there is much better ways to run this when encountering large data sets.

Comment for reference:

This solution is O(N*M) where N and M are the sizes of flat and nested. You can do this in O(MAX(N, M)). Basically, loop over nested instead of result, and do for a,b in nested: result[a].append(b)

Running the above code, 1000 times, with 1000 elements in the flat list and 1000 nested lists in nested.. the run time is;

print(t.timeit(1000))
39.37227249999978

However running it with the below code boasts a much more efficient run time.

print(j.timeit(1000))
0.17638869999973394

Code:

result= {key: [] forkeyinflat}
fora,b in nested:if a in result:result[a].append(b)

Post a Comment for "Comparing List With A List Of Lists"