Commoner
Commoner

Reputation: 1768

Compare a list of lists with a list

I have a (Python) list 'A' and corresponding to many (but not all) elements of that list I have specific numbers. For example, I have the following list A

A = [2, 4, 6, 8, 10, 12]

And, the entries of the list A which are in boldface (i.e. 4, 8 and 10) have the respective values (5, 25, 55) associated with them, while the other entries of list A (i.e. 2, 6, 12) do not have any values associated with them.

I am able to make a list of lists in Python for the entries that have values associated with them. Like

ListofLists = [[4, 5], [8, 25], [10, 55]] 

The source of the "associated values" (5,25,55) is ListofLists and it has to be compared with the list A. As shown in the example, I expect to find entries in list A that do not have any values attached with them (like missing values) and I want to fix that.

I want to fill zeros as values for the entries in list A which do not have any associated values, and by comparing 'ListofLists' with 'A', I want to come up with a new ListofLists which should read

ListofLists_new = [[2, 0], [4, 5], [6, 0], [8, 25], [10, 55], [12, 0]]

Upvotes: 0

Views: 933

Answers (6)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48120

Assuming that the associated values are mapped using dict like:

associated_values = {8: 25, 10: 55, 4: 5}

# you may get this `dict` via. type-casting `ListofLists` to `dict` as:
#     associated_values = dict(ListofLists)

In order to create a mapped list with missing values filled with 0, you may use dict.get(key, 0) along with list comprehension expression as:

>>> my_list = [2, 4, 6, 8, 10, 12]

>>> [[v, associated_values.get(v, 0)] for v in my_list]
[[2, 0], [4, 5], [6, 0], [8, 25], [10, 55], [12, 0]]

Upvotes: 2

voglster
voglster

Reputation: 833

You can use a dictionary for this:

A = [2, 4, 6, 8, 10, 12]
ListOfLists = [[4, 5], [8, 25], [10, 55]]
lol_dict = {key:value for key,value in ListOfLists}
out_dict = {key:lol_dict.get(key,0) for key in A}
final_out = [[key,value] for key,value in out_dict.iteritems()]
print final_out

[[2, 0], [4, 5], [6, 0], [8, 25], [10, 55], [12, 0]]

Upvotes: 1

aghast
aghast

Reputation: 15310

All of the suggestions to use a dict are right. If you can't use a dict - because this is homework or something - here's some code that does what I think you want:

#!python3

A = [2, 4, 6, 8, 10, 12]
ListofLists = [[4, 5], [8, 25], [10, 55]]

result = []

for a in A:
    for k,v in ListofLists:
        if a == k:
            result.append([k,v])
            break
    else:
        result.append([a,0])

assert result == [[2, 0], [4, 5], [6, 0], [8, 25], [10, 55], [12, 0]]
print(result)

Upvotes: 1

Nj3
Nj3

Reputation: 119

Considering association is done in a dictionary

assoc = {4:5,8:25,10:55}
A = [2,4,8,6,10,12]
lstoflst = []
for i in A:
    if i in assoc.keys():
        lstoflst.append([i,assoc[i]])
    else:
        lstoflst.append([i,0])
print(lstoflst)

Upvotes: 1

xmcp
xmcp

Reputation: 3752

Turn ListofLists into a dict, and then you can use dict.get. Like this:

A = [2, 4, 6, 8, 10, 12]
li = [[4, 5], [8, 25], [10, 55]] 
li_dict = {k:v for k,v in li}
out = [[a,li_dict.get(a,0)] for a in A]
print(out) # [[2, 0], [4, 5], [6, 0], [8, 25], [10, 55], [12, 0]]

(I'm not sure if this is what you want)

Upvotes: 1

Sangeon Park
Sangeon Park

Reputation: 188

Why don't you use a dict, it should do your job well.

First, create a dict similar to your ListofLists, using the first element as key and second element as value for each entries.

Then using dict.get(key, default_value) will be a much more elegant solution. In your case, dict.get(key,0) would suffice.

Upvotes: 1

Related Questions