Hi,
With a list of fixed length strings, I want to count the occurrences of each characters at each of 9 positions. I then want to return the top 2 results for each position. The result has to be a list for the function I am passing this too. The code I have so far has two rather big problems (1) it is too slow and (2) it gives the wrong results :(
[CODE=python]
dd ={'.LEA.....':7 7,'R....L...':8 ,'.L....DA.':5, '.L.R.V..L':4,' A....S.SA':55,' QL..L....':5,'M .SC.SE..':77}
def positionalWeigh ts(dd, topx=2):
posList = [[] for i in range(9)]
for key in dd.keys():
for i, item in enumerate(key):
if item != '.':
if posList[i]==[]:
posList[i].append([item, 1])
else:
for c in posList[i]:
if c[0] != item:
posList[i].append([item,1])
else:
c[1] += 1
for i in posList:
for j in i:
j.sort()
y =[]
for i in posList:
x = topx
for j, k in enumerate(i):
z=[]
while x > 0:
z.append(k[1])
x-=1
y.append(z)
return y
pw= positionalWeigh ts(dd)
print pw
>>>
[['A', 'A'], [], [], [], [], [], [], [], ['L', 'L'], ['S', 'S'], [], ['R', 'R'], [], [], [], ['L', 'L'], ['S', 'S'], [], [], [], [], [], [], ['D', 'D'], [], ['S', 'S'], [], ['A', 'A'], []]
[/CODE]
Please help!
With a list of fixed length strings, I want to count the occurrences of each characters at each of 9 positions. I then want to return the top 2 results for each position. The result has to be a list for the function I am passing this too. The code I have so far has two rather big problems (1) it is too slow and (2) it gives the wrong results :(
[CODE=python]
dd ={'.LEA.....':7 7,'R....L...':8 ,'.L....DA.':5, '.L.R.V..L':4,' A....S.SA':55,' QL..L....':5,'M .SC.SE..':77}
def positionalWeigh ts(dd, topx=2):
posList = [[] for i in range(9)]
for key in dd.keys():
for i, item in enumerate(key):
if item != '.':
if posList[i]==[]:
posList[i].append([item, 1])
else:
for c in posList[i]:
if c[0] != item:
posList[i].append([item,1])
else:
c[1] += 1
for i in posList:
for j in i:
j.sort()
y =[]
for i in posList:
x = topx
for j, k in enumerate(i):
z=[]
while x > 0:
z.append(k[1])
x-=1
y.append(z)
return y
pw= positionalWeigh ts(dd)
print pw
>>>
[['A', 'A'], [], [], [], [], [], [], [], ['L', 'L'], ['S', 'S'], [], ['R', 'R'], [], [], [], ['L', 'L'], ['S', 'S'], [], [], [], [], [], [], ['D', 'D'], [], ['S', 'S'], [], ['A', 'A'], []]
[/CODE]
Please help!
Comment