Python Forum
appending list items to other lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: appending list items to other lists (/thread-14592.html)



appending list items to other lists - clarablanes - Dec-08-2018

Good morning friends,
the whole week trying to do the following:

I have to obtain 16 descriptors of thousands of compounds.
The idea is to create an empty list with rows, each row with the name of a compound and the 16 descriptors about it. I use different programs to obtain these numbers.
I've managed to do it but I have a different output in one of the programs. It gives me a list of numbers and I have to append these numbers to the rows in the general list. Something like this:

output of the program: ['1', '2', '3'...]
original list: [['verapamil', '0.5', '9.2'],['norverapamil', '0.6', '7.3']...]

The question is, how could I append '1' to the row with the 'verapamil', '2' to the row with 'norverapamil' etc?

Thank you very much.


RE: appending list items to other lists - Larz60+ - Dec-08-2018

If you use classes, you can have a master script that instantiates an instance of the class in each of the other modules which will make all of the class methods reachable from the master.
The methods that create the lists can contain a return statement with attribute of the lists
then you can merge in the master.
Here's a (very simplistic) example:

script1.py:
import os

class Script1:
    def __init__(self):
        '''
        the following statement assures that you are in the directory where all scripts 
        are located. The proper way to do this is to use __init__.py, but this will work
        for this example.
        '''
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
    
    def list_1(self):
        self.list = [1,2,3,4]
        return self.list
Script2.py
import os


class Script2:
    def __init__(self):
        '''
        the following statement assures that you are in the directory where all scripts 
        are located. The proper way to do this is to use __init__.py, but this will work
        for this example.
        '''
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
    
    def list_2(self):
        self.list = [7,8,9,10]
        return self.list
Master.py
import os
import Script1
import Script2


class Master:
    def __init__(self):
        '''
        the following statement assures that you are in the directory where all scripts 
        are located. The proper way to do this is to use __init__.py, but this will work
        for this example.
        '''
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        self.script1 = Script1.Script1()
        self.script2 = Script2.Script2()
    
    def merge_lists(self):
        newlist = self.script1.list_1() + self.script2.list_2()
        print('The new list: {}'.format(newlist))

if __name__ == '__main__':
    mm = Master()
    mm.merge_lists()
running Master.py:
Output:
(try_venv) ../TryStuff $ python ../Master.py The new list: [1, 2, 3, 4, 7, 8, 9, 10] (try_venv) ../TryStuff $



RE: appending list items to other lists - clarablanes - Dec-08-2018

Thank you!!


RE: appending list items to other lists - buran - Dec-08-2018

let's keep things simple
output = ['1', '2' ]
original = [['verapamil', '0.5', '9.2'],['norverapamil', '0.6', '7.3']]

for my_list, value in zip(original, output):
    my_list.append(value) # use append to add value at the end of the list
    # my_list.insert(0, value) # use insert(index, value) to insert value at position index

print(original)



RE: appending list items to other lists - clarablanes - Dec-08-2018

very easy this way. Thank you!