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 :( -
dd ={'.LEA.....':77,'R....L...':8,'.L....DA.':5,'.L.R.V..L':4,'A....S.SA':55,'QL..L....':5,'M.SC.SE..':77}
-
-
-
def positionalWeights(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= positionalWeights(dd)
-
print pw
-
-
-
>>>
-
[['A', 'A'], [], [], [], [], [], [], [], ['L', 'L'], ['S', 'S'], [], ['R', 'R'], [], [], [], ['L', 'L'], ['S', 'S'], [], [], [], [], [], [], ['D', 'D'], [], ['S', 'S'], [], ['A', 'A'], []]
-
-
Please help!
8 1474
I think I got the first part working: - 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:
-
found = False
-
for c in posList[i]:
-
if c[0] == item:
-
c[1] += 1
-
found = True
-
if not found:
-
posList[i].append([item,1])
You wern't checking every list in a posList index before you inserted a new list.
I think I got the first part working: - 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:
-
found = False
-
for c in posList[i]:
-
if c[0] == item:
-
c[1] += 1
-
found = True
-
if not found:
-
posList[i].append([item,1])
You wern't checking every list in a posList index before you inserted a new list.
cheers mate, made quite a few mistakes in this one. Finally got it working now- yipee! Still if anyone can propose speed tips, I'm using psyco, and it doesn't seem to be making much difference :S -
def positionalWeights(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([1, item])
-
else:
-
found = False
-
for c in posList[i]:
-
if c[1] == item:
-
c[0] += 1
-
found = True
-
if not found:
-
posList[i].append([1, item])
-
-
for i in posList:
-
i.sort()
-
i.reverse()
-
-
y =[]
-
for i in posList:
-
x = topx
-
z=[]
-
while x>0:
-
z.append(i[x][1])
-
x-=1
-
print z
-
y.append(z)
-
return y
-
-
>>>
-
['Y', 'M']
-
['V', 'M']
-
['L', 'M']
-
['Y', 'E']
-
['L', 'K']
-
['F', 'L']
-
['L', 'Y']
-
['L', 'Y']
-
['I', 'L']
-
[['Y', 'M'], ['V', 'M'], ['L', 'M'], ['Y', 'E'], ['L', 'K'], ['F', 'L'], ['L', 'Y'], ['L', 'Y'], ['I', 'L']]
-
bvdet 2,851
Expert Mod 2GB
See if this helps any: - def positionalWeights(dd, topx=2):
-
posDict = [{} for i in range(len(dd.keys()[0]))]
-
for item in dd.keys():
-
item = list(item)
-
pos = 0
-
while True:
-
try:
-
s = item.pop(0)
-
if s != '.':
-
if posDict[pos].has_key(s):
-
posDict[pos][s] += 1
-
else:
-
posDict[pos][s] = 1
-
pos += 1
-
except: break
-
-
result = []
-
-
for dd in posDict:
-
a = sorted(zip(dd.values(), dd.keys()), reverse=True)
-
print a
-
try:
-
result.append([[a[0][0], a[0][1]]])
-
for i in range(topx-1):
-
result[-1].append([a[i+1][0], a[i+1][1]])
-
except IndexError, e:
-
pass
-
-
return result
-
-
s = positionalWeights(dd, 2)
-
-
for i, item in enumerate(s):
-
for j in item:
-
print 'Position %d: %s = %d' % (i, j[1], j[0])
-
-
Output:
>>> [(1, 'R'), (1, 'Q'), (1, 'M'), (1, 'A')]
[(4, 'L')]
[(1, 'S'), (1, 'E')]
[(1, 'R'), (1, 'C'), (1, 'A')]
[(1, 'L')]
[(2, 'S'), (1, 'V'), (1, 'L')]
[(1, 'E'), (1, 'D')]
[(1, 'S'), (1, 'A')]
[(1, 'L'), (1, 'A')]
Position 0: R = 1
Position 0: Q = 1
Position 1: L = 4
Position 2: S = 1
Position 2: E = 1
Position 3: R = 1
Position 3: C = 1
Position 4: L = 1
Position 5: S = 2
Position 5: V = 1
Position 6: E = 1
Position 6: D = 1
Position 7: S = 1
Position 7: A = 1
Position 8: L = 1
Position 8: A = 1
>>>
See if this helps any: - def positionalWeights(dd, topx=2):
-
posDict = [{} for i in range(len(dd.keys()[0]))]
-
for item in dd.keys():
-
item = list(item)
-
pos = 0
-
while True:
-
try:
-
s = item.pop(0)
-
if s != '.':
-
if posDict[pos].has_key(s):
-
posDict[pos][s] += 1
-
else:
-
posDict[pos][s] = 1
-
pos += 1
-
except: break
-
-
result = []
-
-
for dd in posDict:
-
a = sorted(zip(dd.values(), dd.keys()), reverse=True)
-
print a
-
try:
-
result.append([[a[0][0], a[0][1]]])
-
for i in range(topx-1):
-
result[-1].append([a[i+1][0], a[i+1][1]])
-
except IndexError, e:
-
pass
-
-
return result
-
-
s = positionalWeights(dd, 2)
-
-
for i, item in enumerate(s):
-
for j in item:
-
print 'Position %d: %s = %d' % (i, j[1], j[0])
-
-
Output:
>>> [(1, 'R'), (1, 'Q'), (1, 'M'), (1, 'A')]
[(4, 'L')]
[(1, 'S'), (1, 'E')]
[(1, 'R'), (1, 'C'), (1, 'A')]
[(1, 'L')]
[(2, 'S'), (1, 'V'), (1, 'L')]
[(1, 'E'), (1, 'D')]
[(1, 'S'), (1, 'A')]
[(1, 'L'), (1, 'A')]
Position 0: R = 1
Position 0: Q = 1
Position 1: L = 4
Position 2: S = 1
Position 2: E = 1
Position 3: R = 1
Position 3: C = 1
Position 4: L = 1
Position 5: S = 2
Position 5: V = 1
Position 6: E = 1
Position 6: D = 1
Position 7: S = 1
Position 7: A = 1
Position 8: L = 1
Position 8: A = 1
>>>
Thanks mate, it looks like the speed issue is from another part of the program. I'll definately use parts of this (especially for learning, I need to use try: except more) :)
bvdet 2,851
Expert Mod 2GB
I eliminated one of the try/except blocks by substituting 'while True' for 'while len(item)' and modified the way 'result' is compiled: - def positionalWeights(dd, topx=2):
-
posDict = [{} for i in range(len(dd.keys()[0]))]
-
for item in dd.keys():
-
item = list(item)
-
pos = 0
-
while len(item):
-
s = item.pop(0)
-
if s != '.':
-
if posDict[pos].has_key(s):
-
posDict[pos][s] += 1
-
else:
-
posDict[pos][s] = 1
-
pos += 1
-
result = [[] for i in range(len(dd.keys()[0]))]
-
for j, dd in enumerate(posDict):
-
a = sorted(zip(dd.values(), dd.keys()), reverse=True)
-
try:
-
for i in range(topx):
-
result[j].append([a[i][0], a[i][1]])
-
except IndexError, e:
-
pass
-
return result
I am sure it can be improved.
I eliminated one of the try/except blocks by substituting 'while True' for 'while len(item)' and modified the way 'result' is compiled: - def positionalWeights(dd, topx=2):
-
posDict = [{} for i in range(len(dd.keys()[0]))]
-
for item in dd.keys():
-
item = list(item)
-
pos = 0
-
while len(item):
-
s = item.pop(0)
-
if s != '.':
-
if posDict[pos].has_key(s):
-
posDict[pos][s] += 1
-
else:
-
posDict[pos][s] = 1
-
pos += 1
-
result = [[] for i in range(len(dd.keys()[0]))]
-
for j, dd in enumerate(posDict):
-
a = sorted(zip(dd.values(), dd.keys()), reverse=True)
-
try:
-
for i in range(topx):
-
result[j].append([a[i][0], a[i][1]])
-
except IndexError, e:
-
pass
-
return result
I am sure it can be improved.
Thanks bvdet, definately more concise than my attempt. Had to make some slight changes to it to get the output I wanted. However, there is a really strange property of it, in it that it doesn't return the top topx results, instead it will only return the top 1 for each position regardless of the value of topx. You can however add y to topx where y = top number of results you want -1. Strange indeed! -
def positionalWeights(dd,topx =5):
-
posDict = [{} for i in range(len(dd.keys()[0]))]
-
for item in dd.keys():
-
item = list(item)
-
pos = 0
-
while len(item):
-
s = item.pop(0)
-
if s != '.':
-
if posDict[pos].has_key(s):
-
posDict[pos][s] += 1
-
else:
-
posDict[pos][s] = 1
-
pos += 1
-
result = [[] for i in range(len(dd.keys()[0]))]
-
for j, dd in enumerate(posDict):
-
a = sorted(zip(dd.values(), dd.keys()), reverse=True)
-
try:
-
for i in range(topx+1): # need to add 1 to return top 2
-
result[j].append(a[i][1]) # changed for top topx results, no counts required
-
except IndexError, e:
-
pass
-
return result
-
-
>>>
-
[['F', 'M'], ['L', 'M'], ['A', 'M'], ['F', 'E'], ['Y', 'K'], ['I', 'L'], ['F', 'Y'], ['S', 'Y'], ['V', 'L']]
-
-
bvdet 2,851
Expert Mod 2GB
This code seems to work correctly without adding 1 to 'topx': - def positionalWeights(dd, topx=2):
-
posDict = [{} for i in range(len(dd.keys()[0]))]
-
for item in dd.keys():
-
item = list(item)
-
pos = 0
-
while len(item):
-
s = item.pop(0)
-
if s != '.':
-
if posDict[pos].has_key(s):
-
posDict[pos][s] += 1
-
else:
-
posDict[pos][s] = 1
-
pos += 1
-
print posDict
-
result = [[] for i in range(len(dd.keys()[0]))]
-
for j, dd in enumerate(posDict):
-
a = sorted(zip(dd.values(), dd.keys()), reverse=True)
-
print a
-
try:
-
for i in range(topx):
-
# with counts
-
# result[j].append([a[i][0], a[i][1]])
-
# without counts
-
result[j].append(a[i][1])
-
except IndexError, e:
-
pass
-
return result
-
-
dd ={'.LEA.....':77,'R....L...':8,'.L....DA.':5,'.L.R.V..L':4,\
-
'A....S.SA':55,'QL..L....':5,'M.SC.SE..':77, '.LEADER..':5,\
-
'..LEADER.':5, '.LL..TT..':5, 'AZR..TFGG':5}
-
-
s = positionalWeights(dd, 3)
-
print s
Output: -
>>> [{'A': 2, 'Q': 1, 'R': 1, 'M': 1}, {'Z': 1, 'L': 6}, {'S': 1, 'R': 1, 'E': 2, 'L': 2}, {'A': 2, 'C': 1, 'R': 1, 'E': 1}, {'A': 1, 'D': 1, 'L': 1}, {'E': 1, 'D': 1, 'L': 1, 'S': 2, 'T': 2, 'V': 1}, {'F': 1, 'R': 1, 'E': 2, 'D': 1, 'T': 1}, {'A': 1, 'S': 1, 'R': 1, 'G': 1}, {'A': 1, 'L': 1, 'G': 1}]
-
[(2, 'A'), (1, 'R'), (1, 'Q'), (1, 'M')]
-
[(6, 'L'), (1, 'Z')]
-
[(2, 'L'), (2, 'E'), (1, 'S'), (1, 'R')]
-
[(2, 'A'), (1, 'R'), (1, 'E'), (1, 'C')]
-
[(1, 'L'), (1, 'D'), (1, 'A')]
-
[(2, 'T'), (2, 'S'), (1, 'V'), (1, 'L'), (1, 'E'), (1, 'D')]
-
[(2, 'E'), (1, 'T'), (1, 'R'), (1, 'F'), (1, 'D')]
-
[(1, 'S'), (1, 'R'), (1, 'G'), (1, 'A')]
-
[(1, 'L'), (1, 'G'), (1, 'A')]
-
[['A', 'R', 'Q'], ['L', 'Z'], ['L', 'E', 'S'], ['A', 'R', 'E'], ['L', 'D', 'A'], ['T', 'S', 'V'], ['E', 'T', 'R'], ['S', 'R', 'G'], ['L', 'G', 'A']]
-
>>>
sorry, please disregard my last post, I was being silly again - passing the wrong values to the function. All's good now
Thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Huibuh |
last post by:
I start hating RETURN!!!
list<happy> //#include <list>// is searched via iterator (ptr) so long
until the argument "number" of the class "happy" is equal now.
The function "findhappy" works...
|
by: ~Gee |
last post by:
Hi Folks!
Please see the program below:
1 #include<iostream>
2 #include<list>
3 #include <unistd.h>
4 using namespace std;
5 int main()
6 {
7 {
|
by: Petr Bravenec |
last post by:
I have found that when I use the RETURN NEXT command in recursive function,
not all records are returned. The only records I can obtain from function
are records from the highest level of...
|
by: Shaine Fisher |
last post by:
I have had a look around but I'm either not finding or not seeing the
answer to this, what i want to do is reurn the results as an array, or
some other worldly useful format that flash mx2004 can...
|
by: Stephen Miller |
last post by:
When I dynamically populate a HtmlSelect combo box, the Value property
consistently fails to return the item selected, defaulting instead to
the first item in the list. For example:
Protected...
|
by: Zak McGregor |
last post by:
Hi all
is it possible to get a count(1) statemment, for example here:
select count(1) from results where fixture=4916 and winner=away group by winner;
to return a 0 value instead of...
|
by: Thomas D. |
last post by:
Hello all,
I'm using the IXmlSerializable interface for a project and encounter some
problems when testing my webservice in a client application. I know this
interface is undocumented and not...
|
by: jjturon |
last post by:
Can anyone help me??
I am trying to pass a Select Query variable to a table using Dlookup
and return the value to same select query but to another field.
Ex.
SalesManID ...
|
by: randomtalk |
last post by:
hi, i have the following recursive function (simplified to demonstrate
the problem):
>>> def reTest(bool):
.... result =
.... if not bool:
.... reTest(True)
.... else:
.... print...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |