Python Forum
appending list items to other lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
appending list items to other lists
#1
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.
Reply
#2
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 $
Reply
#3
Thank you!!
Reply
#4
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
very easy this way. Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 197 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,354 May-22-2023, 10:39 PM
Last Post: ICanIBB
  List all possibilities of a nested-list by flattened lists sparkt 1 922 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 885 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,075 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,073 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,657 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,620 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 2,538 May-26-2022, 01:37 PM
Last Post: Mark17
  how to assign items from a list to a dictionary CompleteNewb 3 1,592 Mar-19-2022, 01:25 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020