### Thread: Help on list append!! Have been working on this the whole day!

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
2
Rep Power
0

#### Help on list append!! Have been working on this the whole day!

I have a problem on adding elements to the end of a list.
Actually a_list.append works on some of my elements, but fails in others. They are in the same for loop. I really appreciate it if someone can help me find out why. I have been working on this all day today!

Here it is:

Code:
```for i in range(len(outlist)):
right_nt = []
for m in range(k):
dic = {}
for j in range(max_freq):
nt = outlist[i][j][m]
if nt in dic:
dic[nt] += 1
else:
dic[nt] = 1
print dic
max_value = max(dic.values())
print max_value
if dic[nt] == max_value:
right_nt.append(nt)
kmer = ''.join(right_nt)
print 'kmer is', kmer```
And here is the output:
{'A': 3}
3
{'T': 2, 'C': 1}
2
{'G': 3}
3
{'T': 2, 'A': 1}
2
{'T': 2, 'C': 1}
2

{'G': 3}
3
{'C': 3}
3
kmer is ATGTGC

It supposed to be ATGTTGC. Why is one of the T missing?
Thank you!
Code:
```print('outlist[0]')
print(outlist[0])

print('k')
print(k)

print('max_freq')
print(max_freq)```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
2
Rep Power
0
Originally Posted by b49P23TIvg
Code:
```print('outlist[0]')
print(outlist[0])

print('k')
print(k)

print('max_freq')
print(max_freq)```

Here it is

outlist[0]
['ACGTTGC', 'ATGTCGC', 'ATGATGC']
k
7
max_freq
3

4. Code:
```# the necessary input

outlist = (['ACGTTGC', 'ATGTCGC', 'ATGATGC'],)

# in turn, group is each item of outlist.
# group therefor is a list of sequences

for group in outlist:

# counts is a list of dictionaries.
# Each dictionary has an initial acid count of 0.
# The length of counts is the length of the first sequence in group.

counts = [dict(zip('ACGT',(0,)*4)) for sequence in group[0]]

# count the number of each acid by position in sequence.

for sequence in group:

for (position, acid,) in enumerate(sequence):

counts[position][acid] += 1

# prepare the output list

right_nt = []

# acid_counts accumulates the summary by acid dictionary for each position in turn

for acid_counts in counts:

max_counts = max(acid_counts.values())

# find the acid name associated with the most frequent value

for (key, value,) in acid_counts.items():
if value == max_counts:
break

# and append it to the output list

right_nt.append(key)

# and the rest of the program looks useful
kmer = ''.join(right_nt)
print('kmer is {}'.format(kmer))

# note that you'd need to insert additional code to verify
# the sequences in a group are all the same length, and if
# not then use an algorithm to determine "best alignment".```
Last edited by b49P23TIvg; November 16th, 2013 at 02:41 AM.