Re: Python deepcopy to while statement
2014年6月13日金曜日 12時47分19秒 UTC+9 hito koto: > Hi, all > > > > I want to make the function use while statement,and without a deepcopy > functions. > > > > this is my use deepcopy function correct codes, So, how can i to do a > different way and use while statement: > > > > def foo(x): > > if not isinstance(x, list): > > return x > > return [foo(y) for y in x] I write this code but this is not copy: maybe noe more write while statements: but i can't. def foo(x): y = [] i = len(x)-1 while i >= 0: y.append(x[i]) i -= 1 return y -- https://mail.python.org/mailman/listinfo/python-list
Re: Python deepcopy to while statement
2014年6月13日金曜日 12時47分19秒 UTC+9 hito koto: > Hi, all > > > > I want to make the function use while statement,and without a deepcopy > functions. > > > > this is my use deepcopy function correct codes, So, how can i to do a > different way and use while statement: > > > > def foo(x): > > if not isinstance(x, list): > > return x > > return [foo(y) for y in x] I write this code but this is not copy: maybe have to write one more the while statements: but i can't. def foo(x): y = []> i = len(x)-1 while i >= 0: y.append(x[i]) i -= 1 return y -- https://mail.python.org/mailman/listinfo/python-list
Python deepcopy to while statement
Hi, all I want to make the function use while statement,and without a deepcopy functions. this is my use deepcopy function correct codes, So, how can i to do a different way and use while statement: def foo(x): if not isinstance(x, list): return x return [foo(y) for y in x] -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On 6/12/14 11:57 AM, Chris Angelico wrote: def poplist(L): done = False while done==False: yield L[::-1][:1:] L = L[::-1][1::][::-1] if len(L)==0: done=True Why not just "while L"? OK, here it is with Chris' excellent advice: >>> def poplist(L): while L: yield L[::-1][:1:] L = L[::-1][1::][::-1] >>> L=[1, 2, 3, 4, 5, 6, 7] >>> m=[] >>> pop = poplist(L) >>> for n in poplist(L): m.append(n[0]) >>> m [7, 6, 5, 4, 3, 2, 1] >>> == ah === -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On 6/12/14 11:57 AM, Chris Angelico wrote: On Fri, Jun 13, 2014 at 2:49 AM, Mark H Harris wrote: Consider this generator variation: def poplist(L): done = False while done==False: yield L[::-1][:1:] L = L[::-1][1::][::-1] if len(L)==0: done=True Why not just "while L"? Or are you deliberately trying to ensure that cheating will be detectable? ;-) -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On 6/12/14 11:55 AM, Marko Rauhamaa wrote: while not done: Better Python and not bad English, either. ... and taking Marko's good advice, what I think you really wanted: >>> def poplist(L): done = False while not done: yield L[::-1][:1:] L = L[::-1][1::][::-1] if len(L)==0: done=True >>> L=[1, 2, 3, 4, 5, 6, 7] >>> m=[] >>> pop = poplist(L) >>> for n in poplist(L): m.append(n[0]) >>> m [7, 6, 5, 4, 3, 2, 1] >>> -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
> while done==False: Correction: while not done: Better Python and not bad English, either. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On Fri, Jun 13, 2014 at 2:49 AM, Mark H Harris wrote: > Consider this generator variation: > def poplist(L): > done = False > while done==False: > > yield L[::-1][:1:] > L = L[::-1][1::][::-1] > if len(L)==0: done=True Why not just "while L"? Or are you deliberately trying to ensure that cheating will be detectable? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On 6/11/14 10:12 PM, hito koto wrote: def foo(x): y = [] while x !=[]: y.append(x.pop()) return y Consider this generator variation: >>> def poplist(L): done = False while done==False: yield L[::-1][:1:] L = L[::-1][1::][::-1] if len(L)==0: done=True >>> L1=[1, 2, 3, 4, 5, 6, 7] >>> for n in poplist(L1): print(n) [7] [6] [5] [4] [3] [2] [1] >>> -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On 6/11/14 10:12 PM, hito koto wrote: i want to change this is code: def foo(x): y = [] while x !=[]: y.append(x.pop()) return y Consider this generator (all kinds of permutations on the idea): >>> L1 [1, 2, 3, 4, 5, 6, 7] >>> def poplist(L): while True: yield L[::-1][:1:] L = L[::-1][1::][::-1] >>> pop = poplist(L1) >>> next(pop) [7] >>> next(pop) [6] >>> next(pop) [5] >>> next(pop) [4] >>> next(pop) [3] >>> next(pop) [2] >>> next(pop) [1] >>> next(pop) [] >>> next(pop) [] -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano: > On Wed, 11 Jun 2014 21:56:06 -0700, hito koto wrote: > > > > > I want to use while statement, > > > > > > for example: > > >>>> def foo(x): > > > ... y = [] > > > ... while x !=[]: > > > ... y.append(x.pop()) > > > ... return y > > > ... > > >>>> print foo(a) > > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > >>>> a > > > [] but this is empty > > >>>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, > > >>>> 8, 9],[10]]) > > > > > > I wouldn't use a while statement. The easy way is: > > > > py> a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]] > > py> y = a[::-1] > > py> print y > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > py> print a > > [[1, 2, 3, 4], [5, 6, 7, 8, 9], [10]] > > > > If you MUST use a while loop, then you need something like this: > > > > > > def foo(x): > > y = [] > > index = 0 > > while index < len(x): > > y.append(x[i]) > > i += 1 > > return y > > > > > > This does not copy in reverse order. To make it copy in reverse order, > > index should start at len(x) - 1 and end at 0. > > > > > > > > -- > > Steven Hi, Thank you! -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano: > On Wed, 11 Jun 2014 21:56:06 -0700, hito koto wrote: > > > > > I want to use while statement, > > > > > > for example: > > >>>> def foo(x): > > > ... y = [] > > > ... while x !=[]: > > > ... y.append(x.pop()) > > > ... return y > > > ... > > >>>> print foo(a) > > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > >>>> a > > > [] but this is empty > > >>>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, > > >>>> 8, 9],[10]]) > > > > > > I wouldn't use a while statement. The easy way is: > > > > py> a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]] > > py> y = a[::-1] > > py> print y > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > py> print a > > [[1, 2, 3, 4], [5, 6, 7, 8, 9], [10]] > > > > If you MUST use a while loop, then you need something like this: > > > > > > def foo(x): > > y = [] > > index = 0 > > while index < len(x): > > y.append(x[i]) > > i += 1 > > return y > > > > > > This does not copy in reverse order. To make it copy in reverse order, > > index should start at len(x) - 1 and end at 0. > > > > > > > > -- > > Steven Hi, Steven: Thanks, My goal is to be able to in many ways python Sorry, I was mistake, I want to leave a number of previous (a = [[10], [9, 8, 7, 6, 5], [4, 3, 2, 1]] ) -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
2014年6月12日木曜日 14時43分42秒 UTC+9 Steven D'Aprano: > On Wed, 11 Jun 2014 21:56:06 -0700, hito koto wrote: > > > > > I want to use while statement, > > > > > > for example: > > >>>> def foo(x): > > > ... y = [] > > > ... while x !=[]: > > > ... y.append(x.pop()) > > > ... return y > > > ... > > >>>> print foo(a) > > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > >>>> a > > > [] but this is empty > > >>>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, > > >>>> 8, 9],[10]]) > > > > > > I wouldn't use a while statement. The easy way is: > > > > py> a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]] > > py> y = a[::-1] > > py> print y > > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] > > py> print a > > [[1, 2, 3, 4], [5, 6, 7, 8, 9], [10]] > > > > If you MUST use a while loop, then you need something like this: > > > > > > def foo(x): > > y = [] > > index = 0 > > while index < len(x): > > y.append(x[i]) > > i += 1 > > return y > > > > > > This does not copy in reverse order. To make it copy in reverse order, > > index should start at len(x) - 1 and end at 0. > > > > > > > > -- > > Steven Hi, Steven: Thanks, My goal is to be able to in many ways python Sorry, I was mistake, I want to leave a number of previous (a = [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] ) -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On Wed, 11 Jun 2014 21:56:06 -0700, hito koto wrote: > I want to use while statement, > > for example: >>>> def foo(x): > ... y = [] > ... while x !=[]: > ... y.append(x.pop()) > ... return y > ... >>>> print foo(a) > [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] >>>> a > [] but this is empty >>>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, >>>> 8, 9],[10]]) I wouldn't use a while statement. The easy way is: py> a = [[1, 2, 3, 4],[5, 6, 7, 8, 9],[10]] py> y = a[::-1] py> print y [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] py> print a [[1, 2, 3, 4], [5, 6, 7, 8, 9], [10]] If you MUST use a while loop, then you need something like this: def foo(x): y = [] index = 0 while index < len(x): y.append(x[i]) i += 1 return y This does not copy in reverse order. To make it copy in reverse order, index should start at len(x) - 1 and end at 0. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On Thu, Jun 12, 2014 at 2:56 PM, hito koto wrote: > I want to use while statement, This sounds like homework. Go back to your teacher/tutor for assistance, rather than asking us to do the work for you; or at very least, word your question in such a way that we can help you to learn, rather than just give you the answer. Second problem: You're using Google Groups. This makes your posts messy, especially when you quote someone else's text. Please either fix your posts before sending them, or read and post by some other means, such as the mailing list: https://mail.python.org/mailman/listinfo/python-list ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
2014年6月12日木曜日 12時58分27秒 UTC+9 Chris Angelico: > On Thu, Jun 12, 2014 at 1:40 PM, Vincent Vande Vyvre > > wrote: > > > Le 12/06/2014 05:12, hito koto a écrit : > > > > > >> Hello,all > > >> I'm first time, > > >> > > >> I want to make a while statement which can function the same x.pop () and > > >> without the use of pop、how can i to do? > > >> > > >> i want to change this is code: > > >> > > >> def foo(x): > > >> y = [] > > >> while x !=[]: > > >> y.append(x.pop()) > > >> return y > > > > > > Something like that : > > > > > > def foo(x): > > > return reversed(x) > > > > That doesn't do the same thing, though. Given a list x, the original > > function will empty that list and return a new list in reverse order, > > but yours will return a reversed iterator over the original list > > without changing it. This is more accurate, but still not identical, > > and probably not what the OP's teacher is looking for: > > > > def foo(x): > > y = x[::-1] > > x[:] = [] > > return y > > > > If the mutation of x is unimportant, it can simply be: > > > > def foo(x): > > return x[::-1] > > > > ChrisA I want to use while statement, for example: >>> def foo(x): ... y = [] ... while x !=[]: ... y.append(x.pop()) ... return y ... >>> print foo(a) [[10], [5, 6, 7, 8, 9], [1, 2, 3, 4]] >>> a [] but this is empty >>> so,I want to leave a number of previous (a = [[1, 2, 3, 4],[5, 6, 7, 8, >>> 9],[10]]) -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
On Thu, Jun 12, 2014 at 1:40 PM, Vincent Vande Vyvre wrote: > Le 12/06/2014 05:12, hito koto a écrit : > >> Hello,all >> I'm first time, >> >> I want to make a while statement which can function the same x.pop () and >> without the use of pop、how can i to do? >> >> i want to change this is code: >> >> def foo(x): >> y = [] >> while x !=[]: >> y.append(x.pop()) >> return y > > Something like that : > > def foo(x): > return reversed(x) That doesn't do the same thing, though. Given a list x, the original function will empty that list and return a new list in reverse order, but yours will return a reversed iterator over the original list without changing it. This is more accurate, but still not identical, and probably not what the OP's teacher is looking for: def foo(x): y = x[::-1] x[:] = [] return y If the mutation of x is unimportant, it can simply be: def foo(x): return x[::-1] ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: About python while statement and pop()
Le 12/06/2014 05:12, hito koto a écrit : Hello,all I'm first time, I want to make a while statement which can function the same x.pop () and without the use of pop、how can i to do? i want to change this is code: def foo(x): y = [] while x !=[]: y.append(x.pop()) return y Something like that : def foo(x): return reversed(x) -- Vincent V.V. Oqapy <https://launchpad.net/oqapy> . Qarte <https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager> -- https://mail.python.org/mailman/listinfo/python-list
Re:About python while statement and pop()
hito koto Wrote in message: > Hello,all > I'm first time, > > I want to make a while statement which can function the same x.pop () and > without the use of pop、how can i to do? No idea what the question means. Are you just trying to rewrite the loop in a python implementation where pop is broken? > > i want to change this is code: > > def foo(x): > y = [] > while x !=[]: > y.append(x.pop()) > return y Perhaps you're looking for the extend method. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
About python while statement and pop()
Hello,all I'm first time, I want to make a while statement which can function the same x.pop () and without the use of pop、how can i to do? i want to change this is code: def foo(x): y = [] while x !=[]: y.append(x.pop()) return y -- https://mail.python.org/mailman/listinfo/python-list
Re: Error invalid syntax while statement
15 print("counter: ", counter 16 17 while (end == 0): # <---returns syntax error on this while statement Among other responses, there is no indent after print. should be print() while x: #now indent -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Error invalid syntax while statement
On Fri, Jan 7, 2011 at 8:55 PM, Chris Rebert wrote: > On Fri, Jan 7, 2011 at 9:46 PM, Garland Fulton > wrote: > > > 1 #!/bin/bash/python > > > What is > > wrong with my shebang line? > > Its path is invalid (unless you're using a *very* weird system). > /bin/bash is the bash shell executable; bash is completely unrelated > to Python. Further, /bin/bash is a file, not a directory. > > The shebang for Python is normally one of the following: > #!/usr/bin/env python > #!/usr/bin/python > > Cheers, > Chris > -- > http://blog.rebertia.com > Great I have learned a ton and these questions will not arise again. -- http://mail.python.org/mailman/listinfo/python-list
Re: Error invalid syntax while statement
On Fri, Jan 7, 2011 at 9:46 PM, Garland Fulton wrote: > 1 #!/bin/bash/python > What is > wrong with my shebang line? Its path is invalid (unless you're using a *very* weird system). /bin/bash is the bash shell executable; bash is completely unrelated to Python. Further, /bin/bash is a file, not a directory. The shebang for Python is normally one of the following: #!/usr/bin/env python #!/usr/bin/python Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Error invalid syntax while statement
On Fri, Jan 7, 2011 at 8:28 PM, Ned Deily wrote: > In article > , > Garland Fulton wrote: > > I don't understand what I'm doing wrong i've tried several different > cases > > for what i am doing here. Will someone please point my error out. > > > 15 print("counter: ", counter > > Missing ")" on line 15. > > -- > Ned Deily, > n...@acm.org > > -- > http://mail.python.org/mailman/listinfo/python-list > 1 #!/bin/bash/python 2 import math 3 try: 4 x = int(input("Enter your number: ")) 5 if 0 > x > 2147483647: 6 raise Exception() 7 else: 8 end = 0 9 count = 0 10 count1 = x 11 counter = 0 12 print("end: ", end) 13 print("count: ", count) 14 print("count1: ", count1) 15 print("counter: ", counter) 16 17 while end == 0: # <---returns syntax error on this while statement 18 if(count < x): 19 20 sol = math.pow(count, 2) + math.pow(count1, 2) 21 count += 1 22 count1 -= 1 23 print("end: ", end) 24 print("count: ", count) 25 print("count1: ", count1) 26 print("counter: ", counter) 27 if sol == x: 28 counter += x 29 else: 30 end = 1 31 except Exception as ran: 32print("Value not within range", ran) File "blah.py", line 17 while (end == 0): # <---returns syntax error on this while statement ^ IndentationError: unexpected indent Thank you and I'm sorry for the very blind question, it was because of the missing par-ends I have spent a while on this won't happen again. What is wrong with my shebang line? Thank you for the syntax tips! -- http://mail.python.org/mailman/listinfo/python-list
Re: Error invalid syntax while statement
In article , Garland Fulton wrote: > I don't understand what I'm doing wrong i've tried several different cases > for what i am doing here. Will someone please point my error out. > 15 print("counter: ", counter Missing ")" on line 15. -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Error invalid syntax while statement
On Fri, Jan 7, 2011 at 9:18 PM, Garland Fulton wrote: > I don't understand what I'm doing wrong i've tried several different cases > for what i am doing here. Will someone please point my error out. > Thank you. > > 1 #!/bin/bash/python This shebang undoubtedly erroneous. > 5 if( 0 > x | x > 2147483647): One normally writes that using boolean "or" rather than the bitwise operator. Also, the parentheses are completely unnecessary visual clutter. > 15 print("counter: ", counter Where's the closing parenthesis? > 17 while (end == 0): # > <---returns syntax error on this while statement Always include the exact error message + traceback in the future. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Error invalid syntax while statement
I don't understand what I'm doing wrong i've tried several different cases for what i am doing here. Will someone please point my error out. Thank you. 1 #!/bin/bash/python 2 import math 3 try: 4 x = int(input("Enter your number: ")) 5 if( 0 > x | x > 2147483647): 6 raise Exception() 7 else: 8 end = 0 9 count = 0 10 count1 = x 11 counter = 0 12 print("end: ", end) 13 print("count: ", count) 14 print("count1: ", count1) 15 print("counter: ", counter 16 17 while (end == 0): # <---returns syntax error on this while statement 18 if(count < x): 19 20 sol = math.pow(count, 2) + math.pow(count1, 2) 21 count += 1 22 count1 -= 1 23 print("end: ", end) 24 print("count: ", count) 25 print("count1: ", count1) 26 print("counter: ", counter 27 if( sol == x): 28 counter += x 29 else: 30 end = 1 31 except Exception as ran: 32print("Value not within range", ran) -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
On Fri, 22 May 2009 21:33:05 +1000 Joel Ross wrote: > changed it to "float(number)/total*100" and it worked thanks for all > your help appreciated I believe operator.truediv function also deserves a mention here, since line "op.truediv(number, total) * 100" somehow seem to make more sense to me than an explicit conversion. There's also "op.itruediv" for "number /= float(total) * 100" case. http://docs.python.org/dev/library/operator.html -- Mike Kazantsev // fraggod.net signature.asc Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
On Fri, 2009-05-22 at 09:59 -0400, Dave Angel wrote: > > Tim Wintle wrote: > > On Fri, 2009-05-22 at 13:19 +0200, Andre Engels wrote: > > > >> number/total = 998/999 = 0 > >> number/total*100 = 0*100 = 0 > >> float(number/total*100) = float(0) = 0.0 > >> > >> Change "float(number/total*100)" to "float(number)/total*100" and it > >> should work: > >> > > > > I'd use: > > > > (number * 100.)/total > > > > - works because > > * => > > > > It's a minor thing, but it's much faster to cast implicitly as you miss > > the python function call overhead - it's no extra work to write, and for > > numerical things it can really speed things up. > > > > > a = timeit.Timer("float(200)/5*100") > b = timeit.Timer("(200*100.)/5") > a.timeit(1000) > > > 12.282480955123901 > > > b.timeit(1000) > > > 3.6434230804443359 > > > > Tim W > > > > > > > > > It's the old-timer in me, but I'd avoid the float entirely. You start > with ints, and you want to end with ints. So simply do the multiply > first, then the divide. > > number * 100/total > > will get the same answer. > Also, to get the same behavior (faster integer division) in python 3 or when using `from __future__ import division`, use the // division operator instead of /. In fact, a little experimentation shows that you get the speedup even when using python 2.6 with old-style division. Probably because python doesn't have to check the types of its arguments before deciding to do integer division. >>> a = timeit.Timer("(200 * 100.)/5") >>> b = timeit.Timer("(200 * 100)/5") >>> c = timeit.Timer("(200 * 100)//5") >>> d = timeit.Timer("(200 * 100.0)//5") >>> for each in a, b, c, d: ... each.timeit() ... 2.3681092262268066 2.417525053024292 0.81031703948974609 0.81548619270324707 Cheers, Cliff -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
On Fri, 2009-05-22 at 09:59 -0400, Dave Angel wrote: > > Tim Wintle wrote: > > On Fri, 2009-05-22 at 13:19 +0200, Andre Engels wrote: > >> Change "float(number/total*100)" to "float(number)/total*100" and it > >> should work: > >> > > > > I'd use: > > > > (number * 100.)/total > > > > - works because > > * => > It's the old-timer in me, but I'd avoid the float entirely. You start > with ints, and you want to end with ints. So simply do the multiply > first, then the divide. > > number * 100/total Agreed, to be honest I'd not fully read the original post and didn't realise everything was an int. > > will get the same answer. > -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
Dave Angel wrote: Tim Wintle wrote: On Fri, 2009-05-22 at 13:19 +0200, Andre Engels wrote: number/total = 998/999 = 0 number/total*100 = 0*100 = 0 float(number/total*100) = float(0) = 0.0 Change "float(number/total*100)" to "float(number)/total*100" and it should work: I'd use: (number * 100.)/total - works because * => It's a minor thing, but it's much faster to cast implicitly as you miss the python function call overhead - it's no extra work to write, and for numerical things it can really speed things up. a = timeit.Timer("float(200)/5*100") b = timeit.Timer("(200*100.)/5") a.timeit(1000) 12.282480955123901 b.timeit(1000) 3.6434230804443359 Tim W It's the old-timer in me, but I'd avoid the float entirely. You start with ints, and you want to end with ints. So simply do the multiply first, then the divide. number * 100/total will get the same answer. Cheers thanks for the support. if it works faster I'll definitely do it -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
Tim Wintle wrote: On Fri, 2009-05-22 at 13:19 +0200, Andre Engels wrote: number/total = 998/999 = 0 number/total*100 = 0*100 = 0 float(number/total*100) = float(0) = 0.0 Change "float(number/total*100)" to "float(number)/total*100" and it should work: I'd use: (number * 100.)/total - works because * => It's a minor thing, but it's much faster to cast implicitly as you miss the python function call overhead - it's no extra work to write, and for numerical things it can really speed things up. a = timeit.Timer("float(200)/5*100") b = timeit.Timer("(200*100.)/5") a.timeit(1000) 12.282480955123901 b.timeit(1000) 3.6434230804443359 Tim W It's the old-timer in me, but I'd avoid the float entirely. You start with ints, and you want to end with ints. So simply do the multiply first, then the divide. number * 100/total will get the same answer. -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
On Fri, 2009-05-22 at 13:19 +0200, Andre Engels wrote: > number/total = 998/999 = 0 > number/total*100 = 0*100 = 0 > float(number/total*100) = float(0) = 0.0 > > Change "float(number/total*100)" to "float(number)/total*100" and it > should work: I'd use: (number * 100.)/total - works because * => It's a minor thing, but it's much faster to cast implicitly as you miss the python function call overhead - it's no extra work to write, and for numerical things it can really speed things up. >>> a = timeit.Timer("float(200)/5*100") >>> b = timeit.Timer("(200*100.)/5") >>> a.timeit(1000) 12.282480955123901 >>> b.timeit(1000) 3.6434230804443359 Tim W -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
Andre Engels wrote: On Fri, May 22, 2009 at 12:35 PM, Joel Ross wrote: Im using 2.6 python and when running this class progess(): def __init__(self, number, total, char): percentage = float(number/total*100) percentage = int(round(percentage)) char = char * percentage print char progess(100, 999, "*") the "percentage = float(number/total*100)" always equals 0.0 any ideas way this would be? You have to make the conversion of integer to float before the division. What you are calculating now is (take number = 998, total = 999) number/total = 998/999 = 0 number/total*100 = 0*100 = 0 float(number/total*100) = float(0) = 0.0 Change "float(number/total*100)" to "float(number)/total*100" and it should work: float(number) = float(998) = 998.0 float(number)/total = 998.0/999 = 0.99899899899899902 float(number)/total*100 = 0.99899899899899902 * 100 = 99.899899899899902 changed it to "float(number)/total*100" and it worked thanks for all your help appreciated thanks again -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
On Fri, May 22, 2009 at 12:35 PM, Joel Ross wrote: > Im using 2.6 python and when running this > > class progess(): > > def __init__(self, number, total, char): > > percentage = float(number/total*100) > percentage = int(round(percentage)) > char = char * percentage > print char > > progess(100, 999, "*") > > the "percentage = float(number/total*100)" always equals 0.0 > any ideas way this would be? You have to make the conversion of integer to float before the division. What you are calculating now is (take number = 998, total = 999) number/total = 998/999 = 0 number/total*100 = 0*100 = 0 float(number/total*100) = float(0) = 0.0 Change "float(number/total*100)" to "float(number)/total*100" and it should work: float(number) = float(998) = 998.0 float(number)/total = 998.0/999 = 0.99899899899899902 float(number)/total*100 = 0.99899899899899902 * 100 = 99.899899899899902 -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
RE: While Statement
> Im using 2.6 python and when running this > > class progess(): > > def __init__(self, number, total, char): > > percentage = float(number/total*100) > percentage = int(round(percentage)) > char = char * percentage > print char > > progess(100, 999, "*") > > the "percentage = float(number/total*100)" always equals 0.0 Try percentage = float(number)/total*100 # The closing bracket position is the important one The problem is that you're converting to float after the int division damage has been done. Cheers, Drea -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
Andre Engels wrote: On Fri, May 22, 2009 at 11:17 AM, Joel Ross wrote: Hi all, I have this piece of code class progess(): def __init__(self, number, char): total = number percentage = number while percentage > 0 : percentage = int(number/total*100) number-=1 char+="*" print char progess(999, "*") Just wondering if anyone has any ideas on way the percentage var gets set to the value 0 after the first loop. Any feed back would be appreciated. In Python 2.6 and lower, division of two integers gives an integer, being the result without rest of division with rest: 4/3 1 5/3 1 6/3 2 In your example, the second run has number = 998, total = 999. 998/999 is evaluated to be zero. There are two ways to change this: 1. Ensure that at least one of the things you are using in the division is a float. You could for example replace "total = number" by "total = float(number)" or "number/total*100" by "float(number)/total*100" or by "(number*100.0)/total". 2. Use Python 3 behaviour here, which is done by putting the import "from __future__ import division" in your code. Im using 2.6 python and when running this class progess(): def __init__(self, number, total, char): percentage = float(number/total*100) percentage = int(round(percentage)) char = char * percentage print char progess(100, 999, "*") the "percentage = float(number/total*100)" always equals 0.0 any ideas way this would be? -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
Joel Ross wrote: Hi all, I have this piece of code class progess(): def __init__(self, number, char): total = number percentage = number while percentage > 0 : percentage = int(number/total*100) number-=1 char+="*" print char progess(999, "*") Just wondering if anyone has any ideas on way the percentage var gets set to the value 0 after the first loop. Any feed back would be appreciated. Regards jross Thanks for the quick response and the heads up. Makes sense Thank you. You guys helped me out alot:) -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
On Fri, May 22, 2009 at 11:17 AM, Joel Ross wrote: > Hi all, > > I have this piece of code > > class progess(): > > def __init__(self, number, char): > > total = number > percentage = number > while percentage > 0 : > percentage = int(number/total*100) > number-=1 > char+="*" > print char > > progess(999, "*") > > Just wondering if anyone has any ideas on way the percentage var gets set to > the value 0 after the first loop. > > Any feed back would be appreciated. In Python 2.6 and lower, division of two integers gives an integer, being the result without rest of division with rest: >>> 4/3 1 >>> 5/3 1 >>> 6/3 2 >>> In your example, the second run has number = 998, total = 999. 998/999 is evaluated to be zero. There are two ways to change this: 1. Ensure that at least one of the things you are using in the division is a float. You could for example replace "total = number" by "total = float(number)" or "number/total*100" by "float(number)/total*100" or by "(number*100.0)/total". 2. Use Python 3 behaviour here, which is done by putting the import "from __future__ import division" in your code. -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: While Statement
On Fri, May 22, 2009 at 2:47 PM, Joel Ross wrote: > Hi all, > > I have this piece of code > > class progess(): > > def __init__(self, number, char): > > total = number > percentage = number > while percentage > 0 : > percentage = int(number/total*100) > number-=1 > char+="*" > print char > > progess(999, "*") > > Just wondering if anyone has any ideas on way the percentage var gets set to > the value 0 after the first loop. > Put in a from __future__ import division statement at the start. You can experiment in the python shell if you'd like. >>> 2/3 0 >>> from __future__ import division >>> 2/3 0.3 >>> This kind of division is the default in Python 3. -- kushal -- http://mail.python.org/mailman/listinfo/python-list
While Statement
Hi all, I have this piece of code class progess(): def __init__(self, number, char): total = number percentage = number while percentage > 0 : percentage = int(number/total*100) number-=1 char+="*" print char progess(999, "*") Just wondering if anyone has any ideas on way the percentage var gets set to the value 0 after the first loop. Any feed back would be appreciated. Regards jross -- http://mail.python.org/mailman/listinfo/python-list
Re: having problems with a multi-conditional while statement
"Philip Semanchuk" wrote: 8< nice explanation > Change the "and" to an "or" and you'll get the result you expected. Also google for "De Morgan", or "De Morgan's laws" Almost everybody stumbles over this or one of it's corollaries at least once in their careers. - Hendrik -- With the disappearance of the gas mantle and the advent of the short circuit, man's tranquillity began to be threatened by everything he put his hand on. (James Thurber. First sentence of Sex ex Machina) -- http://mail.python.org/mailman/listinfo/python-list
Re: having problems with a multi-conditional while statement
Thanks for the assistance. I actually realized I was making things more complicated than they needed to be and I really only needed one condition to be met. On Jan 6, 7:42 pm, Ned Deily wrote: > In article > <40a44d6b-c638-464d-b166-ef66496a0...@l16g2000yqo.googlegroups.com>, > > > > "bowman.jos...@gmail.com" wrote: > > Hi, > > > I'm trying to write a multi-conditional while statement, and am having > > problems. I've broken it down to this simple demo. > > > #!/usr/bin/python2.5 > > > condition1 = False > > condition2 = False > > > while not condition1 and not condition2: > > print 'conditions met' > > if condition1: > > condition2 = True > > condition1 = True > > > As I understand it, this should print 'conditions met' twice, however, > > it only prints it once. It seems that once condition1 is made true, > > the whole thing evaluates as true and stops the while loop. > > Are you perhaps expecting that the "while" condition is tested at the > end of the loop? It's not; it is tested at the top of the loop, so, > once the condition evaluates as false, the loop exits. This can even > result in zero trips: > > >>> while False: > > ... print "never" > ... > > > > Unwinding the snippet above: > > >>> condition1 = False > >>> condition2 = False > >>> not condition1 and not condition2 > True > >>> if condition1: > > ... condition2 = True > ...>>> condition1 = True > >>> not condition1 and not condition2 > > False > > # -> while loop exits after 1 trip > > -- > Ned Deily, > n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: having problems with a multi-conditional while statement
In article <40a44d6b-c638-464d-b166-ef66496a0...@l16g2000yqo.googlegroups.com>, "bowman.jos...@gmail.com" wrote: > Hi, > > I'm trying to write a multi-conditional while statement, and am having > problems. I've broken it down to this simple demo. > > #!/usr/bin/python2.5 > > condition1 = False > condition2 = False > > while not condition1 and not condition2: > print 'conditions met' > if condition1: > condition2 = True > condition1 = True > > > As I understand it, this should print 'conditions met' twice, however, > it only prints it once. It seems that once condition1 is made true, > the whole thing evaluates as true and stops the while loop. Are you perhaps expecting that the "while" condition is tested at the end of the loop? It's not; it is tested at the top of the loop, so, once the condition evaluates as false, the loop exits. This can even result in zero trips: >>> while False: ... print "never" ... >>> Unwinding the snippet above: >>> condition1 = False >>> condition2 = False >>> not condition1 and not condition2 True >>> if condition1: ... condition2 = True ... >>> condition1 = True >>> not condition1 and not condition2 False # -> while loop exits after 1 trip -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: having problems with a multi-conditional while statement
On Jan 6, 2009, at 7:18 PM, bowman.jos...@gmail.com wrote: Hi, I'm trying to write a multi-conditional while statement, and am having problems. I've broken it down to this simple demo. #!/usr/bin/python2.5 condition1 = False condition2 = False while not condition1 and not condition2: print 'conditions met' if condition1: condition2 = True condition1 = True As I understand it, this should print 'conditions met' twice, however, it only prints it once. It seems that once condition1 is made true, the whole thing evaluates as true and stops the while loop. Think it through. At the outset: while (not condition1) and (not condition2) ==> while (not False) and (not False) ==> while True and True ==> while True After it's been through the loop once: while (not condition1) and (not condition2) ==> while (not True) and (not False) ==> while False and True ==> while False Change the "and" to an "or" and you'll get the result you expected. -- http://mail.python.org/mailman/listinfo/python-list
having problems with a multi-conditional while statement
Hi, I'm trying to write a multi-conditional while statement, and am having problems. I've broken it down to this simple demo. #!/usr/bin/python2.5 condition1 = False condition2 = False while not condition1 and not condition2: print 'conditions met' if condition1: condition2 = True condition1 = True As I understand it, this should print 'conditions met' twice, however, it only prints it once. It seems that once condition1 is made true, the whole thing evaluates as true and stops the while loop. I've also tried to set the while condition the following ways also, and had the same problem while (not condition1 and not condition2): while (not condition1) and (not condition2): while condition1 != True and condition2 != True: while (condition1 != True and condition2 != True): while (condition1 != True) and (condition2 != True): Can someone lend me a hand in understanding this? -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050112 while statement
> "b" == brianr <[EMAIL PROTECTED]> writes: b> (As a matter of interest, is this sequence of posts intended to b> demonstrate ignorance of both languages, or just one?) Intentional fallacy -- there's no necessary correlation between what he *intends* to do and what he actually succeeds at doing. As noted, Xah *intends* to use his expertise in Perl to teach Python to others. All he's succeeding in doing is demonstrating his incompetence at both. As for myself, I suspect it's just a cunning approach to Let's You And Him Fight. Charlton -- cwilbur at chromatico dot net cwilbur at mac dot com -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050112 while statement
Xah Lee ([EMAIL PROTECTED]) wrote on CLIII September MCMXCIII in news:[EMAIL PROTECTED]>: .. # here's a while statement in python. .. .. a,b = 0,1 .. while b < 20: .. print b IndentationError: expected an indented block .. a,b = b,a+b You have already proven you don't know Perl, but now it turns out, you don't know Python either. Go away. Abigail -- package Z;use overload'""'=>sub{$b++?Hacker:Another}; sub TIESCALAR{bless\my$y=>Z}sub FETCH{$a++?Perl:Just} $,=$";my$x=tie+my$y=>Z;print$y,$x,$y,$x,"\n";#Abigail -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050112 while statement
Xah Lee wrote: # here's a while statement in python. a,b = 0,1 while b < 20: print b a,b = b,a+b --- # here's the same code in perl ($a,$b)=(0,1); while ($b<20) { print $b, "\n"; ($a,$b)= ($b, $a+$b); } Because you're posting this to newsgroups, it would be advisable to use only spaces for indentation -- tabs are removed by a lot of newsreaders, which means your Python readers are going to complain about IndentationErrors. Personally, I think you should not do this over comp.lang.python (and perhaps others feel the same way about comp.lang.perl.misc?) Can't you start a Yahoo group or something for this? What you're doing is probably not of interest to most of the comp.lang.python community, and might me more appropriate in a different venue. Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050112 while statement
Steven Bethard wrote: Xah Lee wrote: [some Python code] Because you're posting this to newsgroups, it would be advisable to use only spaces for indentation -- tabs are removed by a lot of newsreaders, which means your Python readers are going to complain about IndentationErrors. Unfortunately, there are now two ways to post screwed up Python code, and using tabs is only one of them. The other is to post from Google Groups, and that's what Xah Lee is doing. (The rest of your advice, about going away, is pretty good though. ;-) -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050112 while statement
[EMAIL PROTECTED] schrieb: "Xah Lee" <[EMAIL PROTECTED]> writes: [...] (As a matter of interest, is this sequence of posts intended to demonstrate ignorance of both languages, or just one?) :) This sequence of posts is intended to stir up a debate just for the sake of a debate. It's a time sink. It's up to you wether you want to post to this thread or do something useful. :) -- --- Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0 E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64') --- -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050112 while statement
"Xah Lee" <[EMAIL PROTECTED]> writes: > # here's a while statement in python. > > a,b = 0,1 > while b < 20: > print b > a,b = b,a+b > > --- > # here's the same code in perl > > ($a,$b)=(0,1); > while ($b<20) { > print $b, "\n"; > ($a,$b)= ($b, $a+$b); > } That python code produces a syntax error: File "w.py", line 3 print b ^ IndentationError: expected an indented block So, not the same then! (As a matter of interest, is this sequence of posts intended to demonstrate ignorance of both languages, or just one?) -- Brian Raven If you write something wrong enough, I'll be glad to make up a new witticism just for you. -- Larry Wall in <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050112 while statement
Xah Lee wrote: # here's a while statement in python. > [...] > # here's the same code in perl [...] So? -- Michael Hoffman -- http://mail.python.org/mailman/listinfo/python-list
[perl-python] 20050112 while statement
# here's a while statement in python. a,b = 0,1 while b < 20: print b a,b = b,a+b --- # here's the same code in perl ($a,$b)=(0,1); while ($b<20) { print $b, "\n"; ($a,$b)= ($b, $a+$b); } Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list