Re: efficiency question

2006-06-30 Thread MTD
For the sake of comparison: >>> def cod(x): ... tupple1 = ("abc", "def", "xyz") ... tupple2 = ("pqr", "tuv", "123") ... if x in tupple1: ... doStuff() ... elif x in tupple2: ... doOtherStuff() ... >>> dis.dis(cod) 2 0 LOAD_CONST 7 (

Re: style question

2006-06-26 Thread MTD
Hari Sekhon wrote: > Is it better to do: > > message = """This is line1. > This is line2 > This is line3\n""" > > or > > message = "This is line1.\n > message = message + "This is line2\n" > message = message + "This is line3\n" Is there any reason you can't do it in one line? message = "This is

Re: Iteration over recursion?

2006-06-21 Thread MTD
- it's worth the experiment! Cheers, MTD -- http://mail.python.org/mailman/listinfo/python-list

Re: Iteration over recursion?

2006-06-20 Thread MTD
Thanks a lot! More food for thought! -- http://mail.python.org/mailman/listinfo/python-list

Iteration over recursion?

2006-06-20 Thread MTD
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in python is generally more time-efficient than recursion. Is that true? Here is my algorithm as it stands. A

Re: How to truncate/round-off decimal numbers?

2006-06-20 Thread MTD
> The system cannot > accurately represent some integers, Er, I meant FLOATS. Doh. Anyway, just to underline the example: >>> x 0.3 >>> s = str(round(x,2)) >>> s '0.67' >>> f = float(s) >>> f 0.67004 >>> f == round(x,2) True -- http://mail.python.org/mailman/listinf

Re: How to truncate/round-off decimal numbers?

2006-06-20 Thread MTD
> >>> a = 2 > >>> b = 3 > >>> round(a*1.0 / b,2) > 0.67004 > > Inspite of specifying 2 in 2nd attribute of round, it outputs all the > digits after decimal. This is because of floating point inaccuracy. The system cannot accurately represent some integers, however it does its best to a

Re: Extracting values from text file

2006-06-16 Thread MTD
P.S. >>> file.close() MTD wrote: > list.txt is a file that contains the following lines: > Apples 34 > Bananas 10 > Oranges 56 > > >>> file = open("list.txt","r") > >>> mystring = file.read() > >>> mystring > &#x

Re: Extracting values from text file

2006-06-16 Thread MTD
list.txt is a file that contains the following lines: Apples 34 Bananas 10 Oranges 56 >>> file = open("list.txt","r") >>> mystring = file.read() >>> mystring 'Apples 34 \nBananas 10\nOranges 56 ' >>> mylist = mystring.split('\n') >>> mylist ['Apples 34 ', 'Bananas 10', 'Oranges 56 '] >>> mydict =

Re: a string problem

2006-06-13 Thread MTD
> When i specify i only want to print the lines that contains "string" ie > the first line and not the others. If i use re module, how to compile > the expression to do this? I tried the re module and using simple > search() and everytime it gives me all the 3 lines that have "string" > in it, wher

Re: Searching and manipulating lists of tuples

2006-06-12 Thread MTD
> Yes, use the proper tool for the job. Tuples are immutable (they are > read-only once created). Instead use a dictionary. They key would be > your string, the value would be the count. Wow, I really should have thought of that! Thanks. -- http://mail.python.org/mailman/listinfo/python-list

Searching and manipulating lists of tuples

2006-06-12 Thread MTD
Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs e.g. L = [ ("X",1),("Y",2)] would mean "X" occurs once and "Y" occurs twice If I am given a string, I want to search L to

Re: How to link foreign keys & primary keys using python?

2006-06-09 Thread MTD
Your post is confusing. Here is my advice: investigate the use of dictionaries. Dictionaries can allow you to define data in the form { key:data }, e.g. { area_code : area_data } { (area_code,school_code) : school_data } { (school_code,student_code) : student_data } -- http://mail.python.org/m

Re: How to generate k+1 length strings from a list of k length strings?

2006-06-08 Thread MTD
Jon Clements wrote: > Are you asking the question, "Which pairs of strings have one character > different in each?", or "Which pairs of strings have a substring of > len(string) - 1 in common?". > > Jon. I imagine it's the former because the latter is trivially easy, I mean _really_ trivially eas

Re: How to generate k+1 length strings from a list of k length strings?

2006-06-08 Thread MTD
So yeah, just to put it all together, try this. From your two Ks, it either returns K+1 if it can or an empty string. def k2k1(string1, string2): for c in string1: string2 = string2.replace(c,"",1) if len(string2) == 1: string1 += string2 else: string1 = ""

Re: How to generate k+1 length strings from a list of k length strings?

2006-06-08 Thread MTD
actually, minor fix: MTD wrote: > Try this: > > def k2k1(string1, string2): > for c in string1: > string2 = string2.replace(c,"",1) > > if len(string2) == 1: > string1 += string2 else: string1 = "" >

Re: How to generate k+1 length strings from a list of k length strings?

2006-06-08 Thread MTD
Try this: def k2k1(string1, string2): for c in string1: string2 = string2.replace(c,"",1) if len(string2) == 1: string1 += string2 return string1 print k2k1("abcd", "ebcd") -- http://mail.python.org/mailman/listinfo/python-list