Re: For Loop Dilema [python-list]

2018-02-26 Thread arya . kumar2494
On Monday, February 26, 2018 at 12:57:25 PM UTC+5:30, Chris Angelico wrote: > On Mon, Feb 26, 2018 at 5:19 AM, wrote: > > Why we don’t use: > > > > for _ in _ in _ > > > > Instead of > > > > for _ in _: > > for _ in _: > > > > Ex: > > > > Names = ["Arya","Pupun"] > > > > for name in Names

Re: For Loop Dilema [python-list]

2018-02-26 Thread arya . kumar2494
On Monday, February 26, 2018 at 6:23:24 AM UTC+5:30, Rick Johnson wrote: > On Sunday, February 25, 2018 at 12:19:56 PM UTC-6, arya.ku...@gmail.com wrote: > > > Ex: > > > > Names = ["Arya","Pupun"] > > > > for name in Names: > >for c in name: > >print(c) > > > > instead use: > > > >

Re: For Loop Dilema [python-list]

2018-02-26 Thread arya . kumar2494
On Monday, February 26, 2018 at 6:20:06 AM UTC+5:30, Ian wrote: > On Sun, Feb 25, 2018 at 11:19 AM, wrote: > > Why we don’t use: > > > > for _ in _ in _ > > > > Instead of > > > > for _ in _: > > for _ in _: > > > > Ex: > > > > Names = ["Arya","Pupun"] > > > > for name in Names: > >fo

Re: For Loop Dilema [python-list]

2018-02-26 Thread arya . kumar2494
On Monday, February 26, 2018 at 8:51:35 PM UTC+5:30, Ian wrote: > On Sun, Feb 25, 2018 at 8:05 PM, INADA Naoki wrote: > > https://docs.python.org/3.6/library/itertools.html#itertools.product > > I don't see how you would use itertools.product to do what the OP > asked for. You could use itertools

Re: For Loop Dilema [python-list]

2018-02-26 Thread Ian Kelly
On Sun, Feb 25, 2018 at 8:05 PM, INADA Naoki wrote: > https://docs.python.org/3.6/library/itertools.html#itertools.product I don't see how you would use itertools.product to do what the OP asked for. You could use itertools.chain.from_iterable, though: py> names = ['Jack', 'Susan'] py> list(chai

Re: For Loop Dilema [python-list]

2018-02-25 Thread Chris Angelico
On Mon, Feb 26, 2018 at 5:19 AM, wrote: > Why we don’t use: > > for _ in _ in _ > > Instead of > > for _ in _: > for _ in _: > > Ex: > > Names = ["Arya","Pupun"] > > for name in Names: >for c in name: >print(c) > > instead use: > > for c in name in Names: > print(c) B

Re: For Loop Dilema [python-list]

2018-02-25 Thread INADA Naoki
https://docs.python.org/3.6/library/itertools.html#itertools.product On Mon, Feb 26, 2018 at 3:19 AM, wrote: > Why we don’t use: > > for _ in _ in _ > > Instead of > > for _ in _: > for _ in _: > > Ex: > > Names = ["Arya","Pupun"] > > for name in Names: >for c in name: >print

Re: For Loop Dilema [python-list]

2018-02-25 Thread Rick Johnson
On Sunday, February 25, 2018 at 12:19:56 PM UTC-6, arya.ku...@gmail.com wrote: > Ex: > > Names = ["Arya","Pupun"] > > for name in Names: >for c in name: >print(c) > > instead use: > > for c in name in Names: > print(c) Hmm. Why stop there? bit = ["kibbles"] bits = [bit, bit

Re: For Loop Dilema [python-list]

2018-02-25 Thread Ian Kelly
On Sun, Feb 25, 2018 at 11:19 AM, wrote: > Why we don’t use: > > for _ in _ in _ > > Instead of > > for _ in _: > for _ in _: > > Ex: > > Names = ["Arya","Pupun"] > > for name in Names: >for c in name: >print(c) > > instead use: > > for c in name in Names: > print(c)

RE: for loop iter next if file bad

2016-12-21 Thread Joaquin Alzola
>def return_files(file_list): >""" >Take a list of files and return file when called. > >Calling function to supply attributes >""" >for file in file_list: >with open(os.path.join(dir_path, file), 'rb') as fd: >if os.stat(fd.name).st_size == 0: >

Re: for loop iter next if file bad

2016-12-21 Thread Sayth Renshaw
Ah yes. Thanks ChrisA http://www.tutorialspoint.com/python/python_loop_control.htm The continue Statement: The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop an

Re: for loop iter next if file bad

2016-12-21 Thread Chris Angelico
On Wed, Dec 21, 2016 at 8:47 PM, Sayth Renshaw wrote: > def return_files(file_list): > """ > Take a list of files and return file when called. > > Calling function to supply attributes > """ > for file in file_list: > with open(os.path.join(dir_path, file), 'rb') as fd:

Re: for loop in python

2016-04-28 Thread BartC
On 28/04/2016 10:34, g.v.aar...@skct.edu.in wrote: start_list = [5, 3, 1, 2, 4] square_list = [] # Your code here! for square_list in start_list: x = pow(start_list, 2) square_list.append(x) square_list.sort() print square_list TypeError: unsupported operand type(s) for ** or pow(): 'l

Re: for loop in python

2016-04-28 Thread Peter Otten
g.v.aar...@skct.edu.in wrote: > start_list = [5, 3, 1, 2, 4] > square_list = [] > > # Your code here! > for square_list in start_list: You are iterating over start_list, that's OK. But you are assigning the current value to square_list, a variable name that you already use for the list where y

Re: for loop in python

2016-04-28 Thread Steven D'Aprano
On Thu, 28 Apr 2016 07:34 pm, g.v.aar...@skct.edu.in wrote: > start_list = [5, 3, 1, 2, 4] > square_list = [] Here you set square_list to a list. > # Your code here! > for square_list in start_list: .^ Here you set square_list to each item of the start_list. So the first time ar

Re: for loop

2015-09-20 Thread shiva upreti
On Sunday, September 20, 2015 at 1:34:32 PM UTC+5:30, paul.ant...@gmail.com wrote: > On Sunday, September 20, 2015 at 9:56:06 AM UTC+2, shiva upreti wrote: > > https://ideone.com/BPflPk > > > > Please tell me why 'print s' statement is being executed inside loop, > > though I put it outside. > >

Re: for loop

2015-09-20 Thread shiva upreti
On Sunday, September 20, 2015 at 1:33:57 PM UTC+5:30, Chris Warrick wrote: > On 20 September 2015 at 09:55, shiva upreti wrote: > > https://ideone.com/BPflPk > > > > Please tell me why 'print s' statement is being executed inside loop, > > though I put it outside. > > Please help. I am new to pyt

Re: for loop

2015-09-20 Thread paul . anton . letnes
On Sunday, September 20, 2015 at 9:56:06 AM UTC+2, shiva upreti wrote: > https://ideone.com/BPflPk > > Please tell me why 'print s' statement is being executed inside loop, though > I put it outside. > Please help. I am new to python. Hi! Welcome to python, the most awesome programming language

Re: for loop

2015-09-20 Thread Chris Warrick
On 20 September 2015 at 09:55, shiva upreti wrote: > https://ideone.com/BPflPk > > Please tell me why 'print s' statement is being executed inside loop, though > I put it outside. > Please help. I am new to python. > -- > https://mail.python.org/mailman/listinfo/python-list You have mixed indent

Re: for loop over function that returns a tuple?

2015-09-02 Thread Steven D'Aprano
On Wed, 2 Sep 2015 09:49 pm, Victor Hooi wrote: > I have a function which is meant to return a tuple: > > def get_metrics(server_status_json, metrics_to_extract, line_number): > > return ((timestamp, "serverstatus", values, tags)) > > I also have: > > def create_point(t

Re: for loop over function that returns a tuple?

2015-09-02 Thread Jussi Piitulainen
Steven D'Aprano writes: > On Wed, 2 Sep 2015 09:49 pm, Victor Hooi wrote: > >> I have a function which is meant to return a tuple: >> >> def get_metrics(server_status_json, metrics_to_extract, line_number): >> >> return ((timestamp, "serverstatus", values, tags)) [- -] >> I

Re: for loop over function that returns a tuple?

2015-09-02 Thread Steven D'Aprano
On Wed, 2 Sep 2015 09:49 pm, Victor Hooi wrote: > I have a function which is meant to return a tuple: > > def get_metrics(server_status_json, metrics_to_extract, line_number): > > return ((timestamp, "serverstatus", values, tags)) No need for the second pair of parentheses.

Re: for loop over function that returns a tuple?

2015-09-02 Thread Jussi Piitulainen
Victor Hooi writes: > I have a function which is meant to return a tuple: > > def get_metrics(server_status_json, metrics_to_extract, line_number): > > return ((timestamp, "serverstatus", values, tags)) That returns a single tuple of four values. The double parentheses are re

Re: For Loop in List

2013-01-13 Thread Mitya Sirenef
On 01/13/2013 07:45 AM, subhabangal...@gmail.com wrote: Dear Group, I have a list like, list1=[1,2,3,4,5,6,7,8,9,10,11,12] Now, if I want to take a slice of it, I can. It may be done in, list2=list1[:3] print list2 [1, 2, 3] If I want to iterate the list, I may do as, for i in list1:

Re: For Loop in List

2013-01-13 Thread Tim Chase
On 01/13/13 07:48, Boris FELD wrote: 2013/1/13 Tim Chase : SIZE = 3 for i in range(len(list1)//SICE): ... print list1[i*SIZE:i*SIZE+SIZE] A little shorter and simpler version: x = x[1:] for i in range(0,len(x),SIZE): ... print x[i: i+SIZE] Doh, I always forget that range() takes

Re: For Loop in List

2013-01-13 Thread Boris FELD
2013/1/13 Tim Chase : > On 01/13/13 06:45, subhabangal...@gmail.com wrote: > SIZE = 3 for i in range(len(list1)//SICE): > ... print list1[i*SIZE:i*SIZE+SIZE] > ... > [1, 2, 3] > [4, 5, 6] > [7, 8, 9] > [10, 11, 12] > A little shorter and simpler version: >>> x = x[1:] >>> for i in ra

Re: For Loop in List

2013-01-13 Thread Tim Chase
On 01/13/13 06:45, subhabangal...@gmail.com wrote: Dear Group, I have a list like, list1=[1,2,3,4,5,6,7,8,9,10,11,12] Now, if I want to take a slice of it, I can. It may be done in, list2=list1[:3] print list2 [snip] Now, I want to combine iterator with a slicing condition like for i=li

Re: For Loop in List

2013-01-13 Thread Dave Angel
On 01/13/2013 07:45 AM, subhabangal...@gmail.com wrote: > Dear Group, > > I have a list like, > list1=[1,2,3,4,5,6,7,8,9,10,11,12] What version of Python? > Now, if I want to take a slice of it, I can. > It may be done in, list2=list1[:3] print list2 > [1, 2, 3] > > If I want to it

Re: for-loop on cmd-line

2012-10-11 Thread Gisle Vanem
"Dave Angel" wrote: Why would you write some C-program just to save having two separate files, one batch and one for the script? For that matter, several answers have given you approaches that didn't involve list comprehensions, including merging the two in a single file, using an initial vari

Re: for-loop on cmd-line

2012-10-11 Thread Dave Angel
On 10/11/2012 09:40 AM, Gisle Vanem wrote: > "Dave Angel" wrote: > >> it has nothing to do with being on a command line. You're using >> semicolon to combine several statements, and there are restrictions on >> what can be combined that way. One restriction is the looping >> constructs, for, if,

RE: for-loop on cmd-line

2012-10-11 Thread Prasad, Ramit
Chris Angelico wrote: > On Fri, Oct 12, 2012 at 3:49 AM, Gisle Vanem wrote: > > wrote in comp.lang.python > > > > (my ISP no longer updates this group. Last message is from 8. April. > > Does the postings to the python mailing-list automatically get reposted to > > comp.lang.python?) > > Yes, c.

Re: for-loop on cmd-line

2012-10-11 Thread Chris Angelico
On Fri, Oct 12, 2012 at 3:49 AM, Gisle Vanem wrote: > wrote in comp.lang.python > > (my ISP no longer updates this group. Last message is from 8. April. > Does the postings to the python mailing-list automatically get reposted to > comp.lang.python?) Yes, c.l.p and python-list mirror each other.

Re: for-loop on cmd-line

2012-10-11 Thread Gisle Vanem
wrote in comp.lang.python (my ISP no longer updates this group. Last message is from 8. April. Does the postings to the python mailing-list automatically get reposted to comp.lang.python?) C:\Windows\system32\python32.zip c:\python32\DLLs I see a similar result: f:\Windows\system32\python

Re: for-loop on cmd-line

2012-10-11 Thread Chris Angelico
On Fri, Oct 12, 2012 at 3:24 AM, wrote: > Le jeudi 11 octobre 2012 15:16:33 UTC+2, Ramchandra Apte a écrit : > > PS C:\> $cmd="import sys;" > PS C:\> $cmd+="print('\n'.join(sys.path))" > PS C:\> $cmd > import sys;print('\n'.join(sys.path)) > PS C:\> c:\python32\python -c $cmd > > C:\Windows\syste

Re: for-loop on cmd-line

2012-10-11 Thread wxjmfauth
Le jeudi 11 octobre 2012 15:16:33 UTC+2, Ramchandra Apte a écrit : PS C:\> $cmd="import sys;" PS C:\> $cmd+="print('\n'.join(sys.path))" PS C:\> $cmd import sys;print('\n'.join(sys.path)) PS C:\> c:\python32\python -c $cmd C:\Windows\system32\python32.zip c:\python32\DLLs c:\python32\lib c:\pytho

Re: for-loop on cmd-line

2012-10-11 Thread Gisle Vanem
"Dave Angel" wrote: it has nothing to do with being on a command line. You're using semicolon to combine several statements, and there are restrictions on what can be combined that way. One restriction is the looping constructs, for, if, while. Ok, I suspected something like that. You can

Re: for-loop on cmd-line

2012-10-11 Thread Chris Angelico
On Fri, Oct 12, 2012 at 12:16 AM, Ramchandra Apte wrote: > What about the "Power" in PowerShell? What about it? Are you suggesting that the OP use it? Are you saying that Windows batch already includes it? You quoted my entire post (double-spaced), but that context adds nothing to your statement;

Re: for-loop on cmd-line

2012-10-11 Thread Ramchandra Apte
On Thursday, 11 October 2012 18:44:44 UTC+5:30, Chris Angelico wrote: > On Thu, Oct 11, 2012 at 11:16 PM, D'Arcy J.M. Cain wrote: > > > On Thu, 11 Oct 2012 13:24:22 +0200 > > > Gisle Vanem wrote: > > > > > >> Hello list. I'm a newbie when it comes to Python. > > >> > > >> I'm trying to tur

Re: for-loop on cmd-line

2012-10-11 Thread Chris Angelico
On Thu, Oct 11, 2012 at 11:16 PM, D'Arcy J.M. Cain wrote: > On Thu, 11 Oct 2012 13:24:22 +0200 > Gisle Vanem wrote: > >> Hello list. I'm a newbie when it comes to Python. >> >> I'm trying to turn this: >> >> def print_sys_path(): >> i = 0 >> for p in sys.path: >> print ('sys.path[%

Re: for-loop on cmd-line

2012-10-11 Thread D'Arcy J.M. Cain
On Thu, 11 Oct 2012 13:24:22 +0200 Gisle Vanem wrote: > Hello list. I'm a newbie when it comes to Python. > > I'm trying to turn this: > > def print_sys_path(): > i = 0 > for p in sys.path: > print ('sys.path[%2d]: %s' % (i, p)) > i += 1 > > into a one-line python command

Re: for-loop on cmd-line

2012-10-11 Thread Dave Angel
On 10/11/2012 07:24 AM, Gisle Vanem wrote: > Hello list. I'm a newbie when it comes to Python. > > I'm trying to turn this: > > def print_sys_path(): >i = 0 >for p in sys.path: > print ('sys.path[%2d]: %s' % (i, p)) > i += 1 > > into a one-line python command (in a .bat file): > >

Re: for-loop on cmd-line

2012-10-11 Thread suzaku
According to the document (http://docs.python.org/using/cmdline.html#interface-options), > When called with -c command, it executes the Python statement(s) given as > command. Here command may contain multiple statements separated by newlines. > Leading whitespace is significant in Python statem

Re: for loop: weird behavior

2012-05-04 Thread Terry Reedy
On 5/4/2012 4:33 PM, ferreirafm wrote: Hi there, I simply can't print anything in the second for-loop bellow: # #!/usr/bin/env python import sys filename = sys.argv[1] outname = filename.split('.')[0] + '_pdr.dat' begin = 'Distance distribution' end = 'R

Re: For loop

2012-05-01 Thread Jabba Laci
Hi, Try this: import sys for i in range (1, 5+1): for j in range (i): sys.stdout.write(str(i)) print "print" adds a newline character print "hi", notice the comma, it won't add newline, however it adds a space With sys.stdout.write you can print the way you want, it won't add

Re: For loop

2012-05-01 Thread Daniel
You could also try http://docs.python.org/library/stdtypes.html#str.join like this: for i in range(5): print "".join(str(i) for j in range(i)) -- http://mail.python.org/mailman/listinfo/python-list

Re: For loop

2012-04-30 Thread Terry Reedy
On 4/30/2012 6:41 AM, viral shah wrote: Hi I want to make a pattern like this *1 22 333 5 Python 3: >>> for i in range(1,6): print(i*str(i)) 1 22 333 5 -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list

Re: For loop

2012-04-30 Thread Chris Angelico
On Mon, Apr 30, 2012 at 8:41 PM, viral shah wrote: > for i in range (5): >   for j in range (i): >     print i >   print " " The print command (you're clearly using Python 2 here - it's slightly different in Python 3) by default prints a whole line - that is, it finishes with a newline or '\n'. Y

Re: For loop comprehensions

2011-02-12 Thread Benjamin S Wolf
On Feb 11, 3:47 pm, Westley Martínez wrote: > No, too confusing. Then people'll want compound loops e.g.: > > for a in b if c while d else return x: >     print('Ha ha I'm so clever!') On Feb 11, 6:34 pm, Steven D'Aprano wrote: > There's nothing wrong with writing > > for x in iterable: >     if

Re: For loop comprehensions

2011-02-12 Thread Ian Kelly
On Fri, Feb 11, 2011 at 7:34 PM, Steven D'Aprano wrote: > On Fri, 11 Feb 2011 16:59:52 -0700, Ian Kelly wrote: > >> Why not allow the same thing in for-loop conditions? > > Because new syntax and new language features means more work. Somebody > has to write the code, make sure that it doesn't bre

Re: For loop comprehensions

2011-02-11 Thread Terry Reedy
On 2/11/2011 6:10 PM, Benjamin S Wolf wrote: It occurred to me as I was writing a for loop that I would like to write it in generator comprehension syntax, eg. for a in b if c: Already proposed and rejected. See archives for python-ideas or the gmane.comp.python.ideas mirror. -- Terry Ja

Re: For loop comprehensions

2011-02-11 Thread Steven D'Aprano
On Fri, 11 Feb 2011 16:59:52 -0700, Ian Kelly wrote: > Why not allow the same thing in for-loop conditions? Because new syntax and new language features means more work. Somebody has to write the code, make sure that it doesn't break existing Python code, test it for bugs, change the document

Re: For loop comprehensions

2011-02-11 Thread Ian Kelly
On Fri, Feb 11, 2011 at 4:47 PM, Westley Martínez wrote: > No, too confusing. Then people'll want compound loops e.g.: > > for a in b if c while d else return x: >    print('Ha ha I'm so clever!') I've yet to see anybody arguing for while loops to be nested like that, but we already allow nested

Re: For loop comprehensions

2011-02-11 Thread Westley Martínez
On Fri, 2011-02-11 at 15:10 -0800, Benjamin S Wolf wrote: > It occurred to me as I was writing a for loop that I would like to > write it in generator comprehension syntax, eg. > > for a in b if c: > > rather than using one of the more verbose but allowable syntaxes: > > for a in (x for x in

Re: For loop comprehensions

2011-02-11 Thread Jon Clements
On Feb 11, 11:10 pm, Benjamin S Wolf wrote: > It occurred to me as I was writing a for loop that I would like to > write it in generator comprehension syntax, eg. > >   for a in b if c: > > rather than using one of the more verbose but allowable syntaxes: > >   for a in (x for x in b if c): > >  

Re: For loop searching takes too long!

2010-02-02 Thread Jonathan Gardner
On Jan 29, 7:07 pm, Steven D'Aprano wrote: > On Fri, 29 Jan 2010 14:49:06 -0800, Jonathan Gardner wrote: > > On Jan 28, 3:52 pm, elsa wrote: > > >> I've got a problem with my program, in that the code just takes too > >> long to run. Here's what I'm doing. If anyone has any tips, they'd be > >> m

Re: For loop searching takes too long!

2010-02-01 Thread Steven D'Aprano
On Mon, 01 Feb 2010 14:04:01 -0800, Jonathan Gardner wrote: > Having worked with datasets that are truly enormous, whenever I see a > list that is unbounded (as this appears to be), I freak out because > unbounded can be a really, really big number. Might as well write > software that works for th

Re: For loop searching takes too long!

2010-01-29 Thread Steven D'Aprano
On Fri, 29 Jan 2010 14:49:06 -0800, Jonathan Gardner wrote: > On Jan 28, 3:52 pm, elsa wrote: >> >> I've got a problem with my program, in that the code just takes too >> long to run. Here's what I'm doing. If anyone has any tips, they'd be >> much appreciated! >> >> > First of all, don't play wi

Re: For loop searching takes too long!

2010-01-29 Thread Jonathan Gardner
On Jan 28, 3:52 pm, elsa wrote: > > I've got a problem with my program, in that the code just takes too > long to run. Here's what I'm doing. If anyone has any tips, they'd be > much appreciated! > First of all, don't play with large lists. Large lists have a tendency to grow larger over time, un

Re: For loop searching takes too long!

2010-01-28 Thread Daniel Stutzbach
On Thu, Jan 28, 2010 at 5:52 PM, elsa wrote: >choice = random.choice(range(1,sum([i[0] for i in myList])+1)) > That's the line causing the problem. It's generating all of the numbers between 1 and your sum, then picking one. Try the following instead, which will pick a number between 1

Re: For loop searching takes too long!

2010-01-28 Thread Dave Angel
Steve Holden wrote: Dave Angel wrote: elsa wrote: Hi guys, I've got a problem with my program, in that the code just takes too long to run. Here's what I'm doing. If anyone has any tips, they'd be much appreciated! So, say I have a list of lists that looks something like this (I'm usi

Re: For loop searching takes too long!

2010-01-28 Thread Steve Holden
Steve Holden wrote: > Dave Angel wrote: >> elsa wrote: >>> Hi guys, >>> >>> I've got a problem with my program, in that the code just takes too >>> long to run. Here's what I'm doing. If anyone has any tips, they'd be >>> much appreciated! >>> >>> So, say I have a list of lists that looks something

Re: For loop searching takes too long!

2010-01-28 Thread Steve Holden
Dave Angel wrote: > elsa wrote: >> Hi guys, >> >> I've got a problem with my program, in that the code just takes too >> long to run. Here's what I'm doing. If anyone has any tips, they'd be >> much appreciated! >> >> So, say I have a list of lists that looks something like this (I'm >> using a lis

Re: For loop searching takes too long!

2010-01-28 Thread Chris Colbert
if you're open to other libraries, this could be done extremely fast in numpy. On my machine summing that whole array takes 75ms. On Thu, Jan 28, 2010 at 8:00 PM, Steven D'Aprano < st...@remove-this-cybersource.com.au> wrote: > On Thu, 28 Jan 2010 15:52:14 -0800, elsa wrote: > > > Now, what I ne

Re: For loop searching takes too long!

2010-01-28 Thread Dave Angel
elsa wrote: Hi guys, I've got a problem with my program, in that the code just takes too long to run. Here's what I'm doing. If anyone has any tips, they'd be much appreciated! So, say I have a list of lists that looks something like this (I'm using a list of lists, rather than a list of tuples

Re: For loop searching takes too long!

2010-01-28 Thread Steven D'Aprano
On Thu, 28 Jan 2010 15:52:14 -0800, elsa wrote: > Now, what I need to do is randomly choose one myList[i], however the > distribution of my random choice needs to be proportional to the values > of myList[i][0]. So, for this list above, I'd have a much higher chance > of choosing myList[0] than my

Re: For loop searching takes too long!

2010-01-28 Thread John Posner
On 1/28/2010 6:52 PM, elsa wrote: Hi guys, I've got a problem with my program, in that the code just takes too long to run. Here's what I'm doing. If anyone has any tips, they'd be much appreciated! So, say I have a list of lists that looks something like this (I'm using a list of lists, rather

Re: For loop searching takes too long!

2010-01-28 Thread Peter Otten
elsa wrote: > Hi guys, > > I've got a problem with my program, in that the code just takes too > long to run. Here's what I'm doing. If anyone has any tips, they'd be > much appreciated! > > So, say I have a list of lists that looks something like this (I'm > using a list of lists, rather than a

Re: for loop: range() result has too many items

2009-10-15 Thread Tim Roberts
Matt Nordhoff wrote: > >That's what itertools.count() is for. > > > >It'll be faster, too, since it's written in C. Not that it really >matters, but it's always nice. For being such a simple concept, I've been surprised how many place

Re: for loop: range() result has too many items

2009-10-15 Thread Mark Dickinson
On Oct 15, 8:39 pm, David C. Ullrich wrote: > >For what it's worth, there *is* a Python oddity lurking > >under the surface here, at least for 64-bit.  Here's > >Python 2.6 on a 64-bit machine: > > Is that a 64-bit Python as well? Yes. Mark -- http://mail.python.org/mailman/listinfo/python-list

Re: for loop: range() result has too many items

2009-10-15 Thread David C . Ullrich
On Wed, 14 Oct 2009 01:54:44 -0700 (PDT), Mark Dickinson wrote: >On Oct 13, 10:39 pm, Steven D'Aprano > wrote: >> On Tue, 13 Oct 2009 16:17:58 -0500, Peng Yu wrote: >> > Hi, >> >> > The following code does not run because range() does not accept a big >> > number. >> >> Incorrect. >> >> >>> range

Re: for loop: range() result has too many items

2009-10-14 Thread Mark Dickinson
On Oct 13, 10:39 pm, Steven D'Aprano wrote: > On Tue, 13 Oct 2009 16:17:58 -0500, Peng Yu wrote: > > Hi, > > > The following code does not run because range() does not accept a big > > number. > > Incorrect. > > >>> range(sys.maxint+2, sys.maxint+5) > > [2147483649L, 2147483650L, 2147483651L] For

Re: for loop: range() result has too many items

2009-10-13 Thread Matt Nordhoff
Matt Nordhoff wrote: > Andre Engels wrote: > [snip] > >> However, I think that the better Python way would be to use a generator: >> >> def infinite_numbergenerator(): >> n = 0 >> while True: >> yield n >> n += 1 >> >> for i in infinite_numbergenerator(): >> ... > >

Re: for loop: range() result has too many items

2009-10-13 Thread Matt Nordhoff
Andre Engels wrote: [snip] > However, I think that the better Python way would be to use a generator: > > def infinite_numbergenerator(): > n = 0 > while True: > yield n > n += 1 > > for i in infinite_numbergenerator(): > ... That's what itertools.count() is for.

Re: Re: for loop: range() result has too many items

2009-10-13 Thread Dave Angel
Andre Engels wrote: On Tue, Oct 13, 2009 at 11:55 PM, Andre Engels wrote: for i in range(sys.maxint): if i % 100 =0: print i Grmbl cut-and-paste error... I meant of course: for i in xrange(sys.maxint): if i % 100 =0: print i What version of Python gives a

Re: for loop: range() result has too many items

2009-10-13 Thread Rhodri James
On Tue, 13 Oct 2009 22:17:58 +0100, Peng Yu wrote: The following code does not run because range() does not accept a big number. Is there a way to make the code work. I'm wondering if there is a way to write a for-loop in python similar to that of C style. xrange() Have you read the document

Re: for loop: range() result has too many items

2009-10-13 Thread Andre Engels
On Tue, Oct 13, 2009 at 11:55 PM, Andre Engels wrote: > for i in range(sys.maxint): >    if i % 100 == 0: >       print i Grmbl cut-and-paste error... I meant of course: for i in xrange(sys.maxint): if i % 100 == 0: print i -- André Engels, andreeng...@gmail.com -- http://mail.p

Re: for loop: range() result has too many items

2009-10-13 Thread Andre Engels
On Tue, Oct 13, 2009 at 11:17 PM, Peng Yu wrote: > Hi, > > The following code does not run because range() does not accept a big > number. Is there a way to make the code work. I'm wondering if there > is a way to write a for-loop in python similar to that of C style. > > for(int i = 0; i < a_big_

Re: for loop: range() result has too many items

2009-10-13 Thread Stephen Hansen
On Tue, Oct 13, 2009 at 2:17 PM, Peng Yu wrote: > Hi, > > The following code does not run because range() does not accept a big > number. Is there a way to make the code work. I'm wondering if there > is a way to write a for-loop in python similar to that of C style. > > for(int i = 0; i < a_big_

Re: for loop: range() result has too many items

2009-10-13 Thread Steven D'Aprano
On Tue, 13 Oct 2009 16:17:58 -0500, Peng Yu wrote: > Hi, > > The following code does not run because range() does not accept a big > number. Incorrect. >>> range(sys.maxint+2, sys.maxint+5) [2147483649L, 2147483650L, 2147483651L] > Is there a way to make the code work. I'm wondering if there

Re: for loop: range() result has too many items

2009-10-13 Thread Mel
Peng Yu wrote: > Hi, > > The following code does not run because range() does not accept a big > number. Is there a way to make the code work. I'm wondering if there > is a way to write a for-loop in python similar to that of C style. > > for(int i = 0; i < a_big_number; ++ i) > > Regards, > Pe

Re: for loop specifying the amount of vars

2008-11-24 Thread Steve Holden
Jules Stevenson wrote: > Hi, > > I have a list which contains a folder structure, for instance: > > dirs=['c:\', 'temp', 'foo', 'bar'] > Of course this should really be dirs=['c:\\', 'temp', 'foo', 'bar'] but we'll overlook your little syntax error ;-) > The length of the list can vary. I'd

RE: for loop specifying the amount of vars

2008-11-24 Thread Jules Stevenson
Sorry for the noise, I found the * unpack operator. Perfect for what I need. > > Hi, > > I have a list which contains a folder structure, for instance: > > dirs=['c:\', 'temp', 'foo', 'bar'] > > The length of the list can vary. I'd like to be able to construct a > os.path.join on the list, but

Re: for loop specifying the amount of vars

2008-11-24 Thread Tim Chase
I have a list which contains a folder structure, for instance: dirs=['c:\', 'temp', 'foo', 'bar'] The length of the list can vary. I'd like to be able to construct a os.path.join on the list, but as the list can vary in length I'm unsure how to do this neatly. Sounds like you want argument un

Re: for loop specifying the amount of vars

2008-11-24 Thread Benjamin Kaplan
On Mon, Nov 24, 2008 at 2:31 PM, Jules Stevenson <[EMAIL PROTECTED]> wrote: > Hi, > > I have a list which contains a folder structure, for instance: > > dirs=['c:\', 'temp', 'foo', 'bar'] > > The length of the list can vary. I'd like to be able to construct a > os.path.join on the list, but as the

Re: For loop inside an xml template

2008-08-25 Thread Amie
On Aug 25, 1:33 pm, Paul Boddie <[EMAIL PROTECTED]> wrote: > On 25 Aug, 10:58, Amie <[EMAIL PROTECTED]> wrote: > > > > > Is it possible to have a for loop within an xml template? > > Yes it is: > > http://www.w3.org/TR/xslt.html#for-each > > Depending on what you mean by "xml template", of course.

Re: For loop inside an xml template

2008-08-25 Thread Paul Boddie
On 25 Aug, 10:58, Amie <[EMAIL PROTECTED]> wrote: > > Is it possible to have a for loop within an xml template? Yes it is: http://www.w3.org/TR/xslt.html#for-each Depending on what you mean by "xml template", of course. Paul -- http://mail.python.org/mailman/listinfo/python-list

Re: For loop inside an xml template

2008-08-25 Thread Simon Brunning
2008/8/25 Amie <[EMAIL PROTECTED]>: > Hi, > > Is it possible to have a for loop within an xml template? Python has a whole bunch of template libraries. Which are you using? -- Cheers, Simon B. [EMAIL PROTECTED] http://www.brunningonline.net/simon/blog/ GTalk: simon.brunning | MSN: small_values |

Re: for loop without variable

2008-01-11 Thread Tim Chase
>> I recently faced a similar issue doing something like this: >> >> data_out = [] >> for i in range(len(data_in)): >> data_out.append([]) > > Another way to write this is > data_out = [[]] * len(data_in) ...if you're willing to put up with this side-effect: >>> data_in = ran

Re: for loop without variable

2008-01-11 Thread Mike Meyer
On Fri, 11 Jan 2008 22:18:22 GMT Neil Hodgson <[EMAIL PROTECTED]> wrote: > Marty: > > I recently faced a similar issue doing something like this: > > data_out = [] > > for i in range(len(data_in)): > > data_out.append([]) > > Another way to write this is > data_out = [[]] * le

Re: for loop without variable

2008-01-11 Thread Neil Hodgson
Marty: > I recently faced a similar issue doing something like this: > > data_out = [] > for i in range(len(data_in)): > data_out.append([]) Another way to write this is data_out = [[]] * len(data_in) Neil -- http://mail.python.org/mailman/listinfo/python-list

Re: for loop without variable

2008-01-11 Thread Paul Rubin
Fredrik Lundh <[EMAIL PROTECTED]> writes: > (and if you use sane naming conventions, the risk for collisions is > near zero as well). I haven't felt that way, I'm always worried about clobbering something by leaking a variable. Maybe collisions don't really happen much, but it's always seemed cle

Re: for loop without variable

2008-01-11 Thread Fredrik Lundh
Paul Rubin wrote: > it just seems way too obscure though. Python style seems to favor > spewing extra variables around. that's because (local) variables have near-zero cost, and zero overhead. use as many as you want, and reuse them as often as you want to. (and if you use sane naming conven

Re: for loop without variable

2008-01-10 Thread Mike Meyer
On Fri, 11 Jan 2008 01:48:43 -0500 Marty <[EMAIL PROTECTED]> wrote: > Mike Meyer wrote: > >> This caused me to wonder why Python does not have a "foreach" statement > >> (and > >> also why has it not come up in this thread)? I realize the topic has > >> probably > >> been beaten to death in ea

Re: for loop without variable

2008-01-10 Thread Basilisk96
On Jan 10, 10:36 pm, Marty <[EMAIL PROTECTED]> wrote: > Hrvoje Niksic wrote: > > Mike Meyer <[EMAIL PROTECTED]> writes: > > >> It sounds to me like your counter variable actually has meaning, > > > It depends how the code is written. In the example such as: > > > for meaningless_variable in xrange

Re: for loop without variable

2008-01-10 Thread Marty
Mike Meyer wrote: > On Thu, 10 Jan 2008 22:36:56 -0500 Marty <[EMAIL PROTECTED]> wrote: >> I recently faced a similar issue doing something like this: >> >> data_out = [] >> for i in range(len(data_in)): >> data_out.append([]) > > More succinctly: > > data_out = [] > for _

Re: for loop without variable

2008-01-10 Thread Carl Banks
On Thu, 10 Jan 2008 22:36:56 -0500, Marty wrote: > I recently faced a similar issue doing something like this: > > data_out = [] > for i in range(len(data_in)): > data_out.append([]) > > This caused me to wonder why Python does not have a "foreach" statement > (and also why has it

Re: for loop without variable

2008-01-10 Thread Paul Rubin
Mike Meyer <[EMAIL PROTECTED]> writes: > data_out = [[] for _ in data_in] > ... > But I'm curious - what's the difference between the "foreach" you have > in mind and the standard python "for"? The "for" loop, like the list comprehension, pollutes the namespace with an index variable that's not us

Re: for loop without variable

2008-01-10 Thread Paul Rubin
erik gartz <[EMAIL PROTECTED]> writes: > The loop performs some actions with web services. The particular > iteration I'm on isn't important to me. It is only important that I > attempt the web services that number of times. If I succeed I > obviously break out of the loop and the containing functi

Re: for loop without variable

2008-01-10 Thread Matimus
On Jan 10, 10:36 pm, Marty <[EMAIL PROTECTED]> wrote: > Hrvoje Niksic wrote: > > Mike Meyer <[EMAIL PROTECTED]> writes: > > >> It sounds to me like your counter variable actually has meaning, > > > It depends how the code is written. In the example such as: > > > for meaningless_variable in xrange

Re: for loop without variable

2008-01-10 Thread Mike Meyer
On Thu, 10 Jan 2008 22:36:56 -0500 Marty <[EMAIL PROTECTED]> wrote: > I recently faced a similar issue doing something like this: > > data_out = [] > for i in range(len(data_in)): > data_out.append([]) More succinctly: data_out = [] for _ in data_in: data_out.append([]) Or, a

  1   2   >