sorry I gave the wrong info
x=1234...
User Profile
Collapse
-
thanks for the reply
I'm summing the squares of elements in a list
Code:x=[1, 2, 3] # I would expect 14 as a result
Leave a comment:
-
type conversion problem
hi all,
I've just started back up with python after a looong break - it's great to be back :D
Anyway, I'm going through "how to think like a computer scientist" as a refresher and trying to come up with a function to return the sum of squares for a given number.
I decided to write it as a list comprehension:
Code:sum(int(i)**2 for i in str(x))
-
-
wxPython crashes when run
Hi,
My GUI keeps crashing on the second time it runs. It doesn't come up with any useful error messages, just says "there was a problem". I've narrowed the offending code to the following.
I'm running wxpython on windows xp.
cheers
[CODE=python]
import wx
class myFrame(wx.Fram e):
def __init__(self, parent, id, title):
wx.Frame.__init __(self,... -
If you want to avoid the use of recursion you can use the following. The factorial is calculated using a while loop, and the summation by a for loop. :
[CODE=python]
>>> def factorial(n):
f=1
while(n>0):
f*=n # same as f=f*n, just faster
n-=1# same as n=n-1
return f
>>> def summation(k, limit):
sigma=0
for i in range(k, limit+1):...Leave a comment:
-
Thanks bvdet, was struggling with this yesterday. There's an extra '-1' in your code, but apart from that, it works fine. The only obscurity is having to pass two parameters for the factorial method - although I understand that this is required for it to be able to be called from the binomail method. Is it better practice to keep it like this, or should it be left in functions? Just looking for an opinion here, still a newb so want to learn the best...Leave a comment:
-
help with object method calls
Hi,
Been reading about objects today. I have a program, that I am trying to rewrite as much as possible as objects. I want to write the following two functions as methods in the Number class:
[CODE=python]
def factorial(n):
'''
n is a positive integer;
RETURNS: integer factorial of n from a recursive function.
'''
f = 1
while (n > 0):
... -
sorry, please disregard my last post, I was being silly again - passing the wrong values to the function. All's good now
ThanksLeave a comment:
-
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!
[CODE=python]
def positionalWeigh ts(dd,topx...Leave a comment:
-
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) :)...Leave a comment:
-
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
[CODE=python]
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 != '.':
...Leave a comment:
-
return top results from list
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}... -
excellent, i was trying to add the tag earlier, just didn't know how!
cheers...Leave a comment:
-
Hi Python101,
I saw from another of your posts that you are either a bioinformaticia n, or working on biological data.
Hopefully you will find some of these links useful:
Quick intro from O'Reilly with useful examples.
O'Reilly Python for Bioinformatics
Beginners guide to Python aimed at Biologists from the Pasteur Institute:
Intro Python for Biologists
More...Leave a comment:
-
Code:>>> str1 = 'test' >>> str2 = 'python' >>> if not str1 in str2: print 'error' error
Leave a comment:
-
-
Hi,
Managed to get this done by modifying KaezarRex's code - thanks.
Code:>>> sList = [['t','v'],['y','k','l']] >>> patt ='..g' >>> def recurse(pattern, sList): if pattern.count('.'): j=pattern.find('.') for i in sList[j]: recurse(pattern.replace ('.', i,1), sList) else: print pattern
Leave a comment:
-
Hi bartonc,
Unfortunately I need to use lists as I need to append values, as in the following (surely there's a more elegant way of doing this):
Code:pos1 =[] pos3 =[] pos4 =[] pos5 =[] pos6 =[] pos7 =[] pos8 =[] for key in gOutList.keys(): if not key[0] in pos1: pos1.append(key[0]) if not key[2] in pos3: pos3.append(key[2])
Leave a comment:
-
Hi all,
Sorry to resurrect an old post, but I have a more complex feature I need to add.
Given some patterns such as "...t...s." I need to make all possible combinations given a separate list for each position. The length of the pattern is fixed to 9, so thankfully that reduces a bit of the complexity.
For example I have the following:
pos1 = ['a',' t']
pos2 = ['r', 's']
...Leave a comment:
No activity results to display
Show More
Leave a comment: