Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Partially merge to list in python

Classic, but i’m new to python… and have a problem a can’t manage to solve. I’m assuming it’s fairly easy.

I have two csv files, one scraped from the web(a=[]), containing 20000+ lines, the other exported from a local system [b=[]] 80+ lines.
I have open the files and stored the data in list a and b. Theey are structured like the example below.

a = [[1,'a','b','a@b',11],
    [2,'c','d','c@b',22],
    [3,'e','f','e@b',33]]

b = [['a','banana','A',100],
    ['e','apple','A',100]]

Now i would like to go through list a and when index 1 of every sublist in list a is equal to index 0 of the sublist in list b it shall append index 3 and 4 of a. So I would end up with

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 c=  [['a','banana','A',100,'a@b',11],
        ['e','apple','A',100,'e@b',33],]

How to achive this. The solution don’t need to be fast if it teaches something about the structure in Python. But if solved easy with pandas i’m all ears.
If this fora is not for questions like this i’m sorry for the time.

>Solution :

This is not optimized and isn’t efficient time complexity vice, but it’s readable and does the job.

c = []
for a_entry in a:
   for b_entry in b:
       if a_entry[1] == b_entry[0]:
           c.append(b_entry + a_entry[3:])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading