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 (
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
- it's worth the experiment!
Cheers,
MTD
--
http://mail.python.org/mailman/listinfo/python-list
Thanks a lot! More food for thought!
--
http://mail.python.org/mailman/listinfo/python-list
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
> 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
> >>> 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
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
>
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 =
> 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
> 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
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
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
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
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 = ""
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 = ""
>
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
17 matches
Mail list logo