Python Forum
Python for loops giving error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for loops giving error
#1
Dear all,

I am new to python programming. I wrote a code, but this code works partially. So what I basically did is writing a dictionary with keys and values. Then writing code to read a file and append all the values from the dictionary that are equivalent to the sequences present in the file.
This works and then I wrote a code to multiply all the values of the list and printing the answers. But this is where I get stuck.
Can someone help me to improve my code so that the end result can be printed?

aa_code = {
    'A': 4, 'C': 2, 'D': 2, 'E': 2, 'F': 2,
    'G': 4, 'H': 2, 'I': 3, 'K': 2, 'L': 6, 
    'M': 1, 'N': 2, 'P': 4, 'Q': 2, 'R': 6, 
    'S': 6, 'T': 4, 'V': 4, 'W': 1, 'Y': 2,
}# this is the dict
from Bio import SeqIO
for protein_seq in SeqIO.parse("protein.fasta","fasta"):
    for i in list(protein_seq):
        combinations = list()
        for value in list(protein_seq):
                combinations.append(aa_code[value])
    print(combinations)# this is the code to get values from the dictionary equivalent to sequence in protein file
answers were:
Output:
[4, 2, 2, 6] [2, 2, 2, 4, 4] [6, 4, 2, 4] [6, 1, 6, 1, 3, 2, 2, 4, 2, 2, 2] [2, 2, 4, 4, 4, 1, 4, 2, 2, 4, 2]
These answers were right as protein sequences were something like this:
VFHL
YKDTT
RTKT
RMLMIDYVCFQ
DKGTPMAEQTY

Then I tried to multiply all the list elements together and I expected to get something like this:
96, 128, 192, 13824, 32768
But my code doesn't work. I tried it like this:

import functools
from Bio import SeqIO
for protein_seq in SeqIO.parse("protein.fasta","fasta"):
    for i in list(protein_seq):
        combinations = list()
        for value in list(protein_seq):
                combinations.append(aa_code[value])
    print(list(combinations))
           for i in list(combinations):
             new_value = list()
        print(functools.reduce(lambda a,b: a*b,(list(combinations[new_value])
So I imported functools so that I can use the functool to reduce or multiply all the elements in the lists. But I am getting error Because my code is not able to recruit all the lists from the list(combinations).
Please help mee!!
Reply


Messages In This Thread
Python for loops giving error - by Petrus - Jan-07-2019, 10:01 PM
RE: Python for loops giving error - by xhughesey - Jan-07-2019, 10:18 PM
RE: Python for loops giving error - by Petrus - Jan-07-2019, 10:56 PM
RE: Python for loops giving error - by perfringo - Jan-08-2019, 05:37 AM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 10:23 AM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 11:38 AM
RE: Python for loops giving error - by perfringo - Jan-08-2019, 11:58 AM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 12:29 PM
RE: Python for loops giving error - by perfringo - Jan-08-2019, 12:48 PM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 01:24 PM
RE: Python for loops giving error - by perfringo - Jan-08-2019, 01:48 PM
RE: Python for loops giving error - by Petrus - Jan-08-2019, 02:27 PM
RE: Python for loops giving error - by Petrus - Jan-09-2019, 08:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  New to python! Loops Seeley307 3 62,175 May-15-2020, 02:27 PM
Last Post: ibreeden
  Error in loops, new to Python jhall710 20 12,396 Apr-25-2017, 05:18 AM
Last Post: smbx33

Forum Jump:

User Panel Messages

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