Hi, I've just started out with Python and I've been stuck on this problem for a few hours now trying to parse a file into a certain format..

I am trying to create a list in a list out of a list.
I currently have this list;

['MPNRRRCKLSTAISTVATLAIASPCAYFLVYEPTASAKPAAKHYEFKQAASIADLPGEVLDAISQGLSQFGINL', 'MQLVDRVRGAVTGMSRRLVVGAVGAALVSGLVGAVGGTATAGAFSRPGLPVEYLQVPSPSMGRSELPGWLQA', 'etc']

What I am trying to make is a list of each item, with the first 40 individual characters in the strings as items, while translating them into an int for example; M = 1.43, P=1.53, I want to be able to have each item as an individual list before putting them into one together because I want to add another value(1/0) on the end of the lists, and in the end put all these items back into a list like this;

[[1.43,'0.00','1.53','1.42','8.90', '1'],[1.43,'0.00','1.53','1.42','8.90', '0'],[1.43,'0.00','1.53','1.42','8.90', '0']]

My current code:

converted = []
seq= ['MPNRRRCKLSTAISTVATLAIASPCAYFLVYEPTASAKPAAKHYEFKQAASIADLPGEVLDAISQGLSQFGINL', 'MQLVDRVRGAVTGMSRRLVVGAVGAALVSGLVGAVGGTATAGAFSRPGLPVEYLQVPSPSMGRSELPGWLQA','GLVGLAGGAATAGAFSRPGLPVEYLQVPSPSMGRDIKVQFQSGGNNSPAVYLLDGLRAQDDYNGWDINTPAFEWACGKAGCQTYKWETFLTSELPQWLSANRAVKPTGSAAIGLSMAGSSAMILAAYHPQQFIYAGSLSALL']

polarity = { 'A': 0.000, 'R': 52.000, 'N':  3.380, 'D': 49.700, 'C':  1.480,  
             'Q': 3.530, 'E': 49.900, 'G':  0.000, 'H': 51.600, 'I':  0.130,  
             'L': 0.130, 'K': 49.500, 'M':  1.430, 'F':  0.350, 'P':  1.580,  
             'S': 1.670, 'T':  1.660, 'W':  2.100, 'Y':  1.610, 'V':  0.130, 
            }

for x in seq:
    seq2 = x[0:39]   
    for y in seq2:
        converted.append(polarity[y]) 
At this point it keeps looping until all items are in the same list, so I could split the list up, but I couldn't the 1/0 value anymore.. also, this program is meant for parsing so the number of items on the first list can vary.

I've already tried a different approach before but I keep getting stuck.
I hope you guys can help me out!

Recommended Answers

All 2 Replies

You append everything to converted as one list, i.e. you have to use a sub-list and append it each time the list breaks.

eq= ['MPNRRRCKLSTAISTVATLAIASPCAYFLVYEPTASAKPAAKHYEFKQAASIADLPGEVLDAISQGLSQFGINL',
      'MQLVDRVRGAVTGMSRRLVVGAVGAALVSGLVGAVGGTATAGAFSRPGLPVEYLQVPSPSMGRSELPGWLQA',
      'GLVGLAGGAATAGAFSRPGLPVEYLQVPSPSMGRDIKVQFQSGGNNSPAVYLLDGLRAQDDYNGWDINTPAFEWACGKAGCQTYKWETFLTSELPQWLSANRAVKPTGSAAIGLSMAGSSAMILAAYHPQQFIYAGSLSALL']

polarity = { 'A': 0.000, 'R': 52.000, 'N':  3.380, 'D': 49.700, 'C':  1.480,  
             'Q': 3.530, 'E': 49.900, 'G':  0.000, 'H': 51.600, 'I':  0.130,  
             'L': 0.130, 'K': 49.500, 'M':  1.430, 'F':  0.350, 'P':  1.580,  
             'S': 1.670, 'T':  1.660, 'W':  2.100, 'Y':  1.610, 'V':  0.130, 
            }

each_list = []
for x in seq:
    converted = []  ## zero elements for each new "x"
    seq2 = x[0:39]   
    for y in seq2:
        converted.append(polarity[y]) 
    each_list.append(converted)  ## as sub-list
for convert in each_list:
    print convert

Thanks for helping me out :)
I ended up with this function:

def Scale():
    seq = getData(raw_input("Enter filename: "))
    each_list = []
    polarity = { 'A': 0.000, 'R': 52.000, 'N':  3.380, 'D': 49.700, 'C':  1.480,  
                 'Q': 3.530, 'E': 49.900, 'G':  0.000, 'H': 51.600, 'I':  0.130,  
                 'L': 0.130, 'K': 49.500, 'M':  1.430, 'F':  0.350, 'P':  1.580,  
                 'S': 1.670, 'T':  1.660, 'W':  2.100, 'Y':  1.610, 'V':  0.130, 
                }
    for item in seq:
        converted = [] 
        seq2 = item[0:39]
        for y in seq2:
            converted.append(polarity[y])    
        each_list.append(converted)

    return each_list
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.