Hi guys,
I need some help with lists or dicts.
I have a list with 2 columns like this

"123.04 241.30","232.00"

[and so on for about 400 lines]

and another list which is more complex:

"val1","val2","val3","nnn.nn nnn.nn","232.00"

(where "n" is a number)
[some thousands lines]

Now I want to compare each item[0] from list1 (let's say the two couple of numbers)
with each item[4] of list2
and restitute the comlete item for list2.

I cannot figure out a way to do it in an efficent way.

Thanks for your help,
Gianluca

Recommended Answers

All 5 Replies

What does restitution in that case mean?
Can you give example input and output?

OK, so list1 should be as follow

14.7992581813 41.5425583232 650.00
14.8021722997 41.5419569232 657.00
14.8009136486 41.5433829232 658.00

and list2

"string1" 2275.2 3323.52 14.8016700793 41.5425025232 n/a 
"string1" 3358.08 253.44 14.8012236611 41.5426947232 n/a 
"string2" 3916.8 1365.12 14.8010810554 41.5413741232 n/a 
"string2" 2401.92 2678.4 14.8009136486 41.5433829232 n/a 
"string2" 2177.28 2327.04 14.8009198488 41.5412501232 n/a 
"string2" 2678.4 2787.84 14.8010004521 41.5412191232 n/a 
"string2" 2154.24 2315.52 14.8009198488 41.5412501232 n/a 
"string2" 2229.12 1618.56 14.8009136486 41.5433829232 n/a 
"string3" 4129.92 2499.84 14.8012174609 41.5414981232 n/a 

With the above example I should get

"string2" 2401.92 2678.4 14.8009136486 41.5433829232 658.00 
"string2" 2229.12 1618.56 14.8009136486 41.5433829232 658.00

because the first 2 values of the 3rd line of list1

14.8009136486 41.5433829232

can be found in 2 lines in list2.

Note also that I ideally would need to substitute n/a in list2 with the value from list1.

Here is pseudo code

for each item in list 1:
    item defines a pair (key, value)
    eg for the item  <14.7992581813 41.5425583232 650.00>,
    the key is <14.7992581813 41.5425583232> and the value
    is <650.00>.

Store all these pairs (key, value) in a dictionary D, and raise
an exception if one of the keys appears twice.

For each item in list 2:
    extract the key from the item, eg the key for
    <"string1" 2275.2 3323.52 14.8016700793 41.5425025232 n/a> is
    <14.8016700793 41.5425025232>.
    If the key is in D, find the corresponding value D[key]. Output <"string1" 2275.2 3323.52 14.8016700793 41.5425025232 value> in this case.

You have to create a dictionary from list1 with the first two items as a key and the last one as a value.
The you should iterate over list2 and if the items before n/a are in the dictionary, then replace n/a with the dictionary value.

list1=[a.split(" ") for a in '''14.7992581813 41.5425583232 650.00
14.8021722997 41.5419569232 657.00
14.8009136486 41.5433829232 658.00'''.split("\n")]

list2=[a.split(" ") for a in '''"string1" 2275.2 3323.52 14.8016700793 41.5425025232 n/a 
"string1" 3358.08 253.44 14.8012236611 41.5426947232 n/a 
"string2" 3916.8 1365.12 14.8010810554 41.5413741232 n/a 
"string2" 2401.92 2678.4 14.8009136486 41.5433829232 n/a 
"string2" 2177.28 2327.04 14.8009198488 41.5412501232 n/a 
"string2" 2678.4 2787.84 14.8010004521 41.5412191232 n/a 
"string2" 2154.24 2315.52 14.8009198488 41.5412501232 n/a 
"string2" 2229.12 1618.56 14.8009136486 41.5433829232 n/a 
"string3" 4129.92 2499.84 14.8012174609 41.5414981232 n/a'''.split("\n")]

dic1=dict()
for line in list1:
    dic1[line[0]+line[1]]=line[2]

for line in list2:
    to_find=line[3]+line[4]
    if to_find in dic1:
        print(" ".join(line[:-1]+[dic1[to_find]]))

Mmhh, I guess it's a good beginning for me.
Thanks
G.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.