For Loop in List

2013-01-13 Thread subhabangalore
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: print Iterated Value Is:,i Iterated Value

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 iterate the list, I

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

Re: For Loop in List

2013-01-13 Thread Boris FELD
2013/1/13 Tim Chase python.l...@tim.thechases.com: 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

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 python.l...@tim.thechases.com: 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

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: loop over list and process into groups

2010-03-05 Thread Paul Rubin
lbolla lbo...@gmail.com writes: for k, g in groupby(clean_up(data) , key=lambda s: s.startswith('VLAN')): if k: key = list(g)[0].replace('VLAN','') This is the nicest solution, I think. Mine was more cumbersome. -- http://mail.python.org/mailman/listinfo/python-list

Re: loop over list and process into groups

2010-03-05 Thread mk
Sneaky Wombat wrote: [ 'VLAN4065', 'Interface', 'Gi9/6', 'Po2', 'Po3', 'Po306', 'VLAN4068', 'Interface', 'Gi9/6', 'VLAN4069', 'Interface', 'Gi9/6',] Hey, I just invented a cute ;-) two-liner using list comprehensions: # alist = list above tmp, dk = [], {} [(x.startswith('VLAN')

Re: loop over list and process into groups

2010-03-05 Thread nn
mk wrote: Sneaky Wombat wrote: [ 'VLAN4065', 'Interface', 'Gi9/6', 'Po2', 'Po3', 'Po306', 'VLAN4068', 'Interface', 'Gi9/6', 'VLAN4069', 'Interface', 'Gi9/6',] Hey, I just invented a cute ;-) two-liner using list comprehensions: # alist = list above tmp,

Re: loop over list and process into groups

2010-03-05 Thread lbolla
On Mar 5, 1:26 pm, mk mrk...@gmail.com wrote: Sneaky Wombat wrote: [ 'VLAN4065',  'Interface',  'Gi9/6',  'Po2',  'Po3',  'Po306',  'VLAN4068',  'Interface',  'Gi9/6',  'VLAN4069',  'Interface',  'Gi9/6',] Hey, I just invented a cute ;-) two-liner using list

Re: loop over list and process into groups

2010-03-05 Thread mk
nn wrote: Oh my! You could have at least used some if else to make it a little bit easier on the eyes :-) That's my entry into 'Obfuscated' Python '''code''' 'contest' and I'm proud of it. ;-) Regards, mk -- http://mail.python.org/mailman/listinfo/python-list

Re: loop over list and process into groups

2010-03-05 Thread mk
lbolla wrote: It looks like Perl ;-) A positive proof that you can write perl code in Python. I, for instance, have had my brain warped by C and tend to write C-like code in Python. That's only half a joke, sadly. I'm trying to change my habits but it's hard. Regards, mk --

loop over list and process into groups

2010-03-04 Thread Sneaky Wombat
[ {'vlan_or_intf': 'VLAN2021'}, {'vlan_or_intf': 'Interface'}, {'vlan_or_intf': 'Po1'}, {'vlan_or_intf': 'Po306'}, {'vlan_or_intf': 'VLAN2022'}, {'vlan_or_intf': 'Interface'}, {'vlan_or_intf': 'Gi7/33'}, {'vlan_or_intf': 'Po1'}, {'vlan_or_intf': 'Po306'}, {'vlan_or_intf': 'VLAN2051'},

Re: loop over list and process into groups

2010-03-04 Thread mk
Sneaky Wombat wrote: I was going to write a def to loop through and look for certain pre- compiled regexs, and then put them in a new dictionary and append to a list, regexes are overkill in this case I think. [ 'VLAN4065', 'Interface', 'Gi9/6', 'Po2', 'Po3', 'Po306', 'VLAN4068',

Re: loop over list and process into groups

2010-03-04 Thread Sneaky Wombat
On Mar 4, 10:55 am, mk mrk...@gmail.com wrote: Sneaky Wombat wrote: I was going to write a def to loop through and look for certain pre- compiled regexs, and then put them in a new dictionary and append to a list, regexes are overkill in this case I think. [ 'VLAN4065',  'Interface',

Re: loop over list and process into groups

2010-03-04 Thread lbolla
On Mar 4, 3:57 pm, Sneaky Wombat joe.hr...@gmail.com wrote: [ {'vlan_or_intf': 'VLAN2021'},  {'vlan_or_intf': 'Interface'},  {'vlan_or_intf': 'Po1'},  {'vlan_or_intf': 'Po306'},  {'vlan_or_intf': 'VLAN2022'},  {'vlan_or_intf': 'Interface'},  {'vlan_or_intf': 'Gi7/33'},  {'vlan_or_intf':

Re: loop over list and process into groups

2010-03-04 Thread nn
lbolla wrote: On Mar 4, 3:57 pm, Sneaky Wombat joe.hr...@gmail.com wrote: [ {'vlan_or_intf': 'VLAN2021'},  {'vlan_or_intf': 'Interface'},  {'vlan_or_intf': 'Po1'},  {'vlan_or_intf': 'Po306'},  {'vlan_or_intf': 'VLAN2022'},  {'vlan_or_intf': 'Interface'},  {'vlan_or_intf':

Re: loop over list and process into groups

2010-03-04 Thread Chris Colbert
Man, deja-vu, I could have sworn I read this thread months ago... On Thu, Mar 4, 2010 at 2:18 PM, nn prueba...@latinmail.com wrote: lbolla wrote: On Mar 4, 3:57 pm, Sneaky Wombat joe.hr...@gmail.com wrote: [ {'vlan_or_intf': 'VLAN2021'}, {'vlan_or_intf': 'Interface'},

loop over list and modify in place

2006-09-30 Thread Daniel Nogradi
Is looping over a list of objects and modifying (adding an attribute to) each item only possible like this? mylist = [ obj1, obj2, obj3 ] for i in xrange( len( mylist ) ): mylist[i].newattribute = 'new value' I'm guessing there is a way to do this without introducing the (in principle

Re: loop over list and modify in place

2006-09-30 Thread Daniel Nogradi
Is looping over a list of objects and modifying (adding an attribute to) each item only possible like this? mylist = [ obj1, obj2, obj3 ] for i in xrange( len( mylist ) ): mylist[i].newattribute = 'new value' I'm guessing there is a way to do this without introducing the (in

Re: loop over list and modify in place

2006-09-30 Thread James Stroud
Daniel Nogradi wrote: Is looping over a list of objects and modifying (adding an attribute to) each item only possible like this? mylist = [ obj1, obj2, obj3 ] for i in xrange( len( mylist ) ): mylist[i].newattribute = 'new value' I'm guessing there is a way to do this without

Re: loop over list and modify in place

2006-09-30 Thread John Machin
James Stroud wrote: Daniel Nogradi wrote: Is looping over a list of objects and modifying (adding an attribute to) each item only possible like this? mylist = [ obj1, obj2, obj3 ] for i in xrange( len( mylist ) ): mylist[i].newattribute = 'new value' I'm guessing there is

Re: loop over list and modify in place

2006-09-30 Thread James Stroud
John Machin wrote: James Stroud wrote: Daniel Nogradi wrote: Is looping over a list of objects and modifying (adding an attribute to) each item only possible like this? mylist = [ obj1, obj2, obj3 ] for i in xrange( len( mylist ) ): mylist[i].newattribute = 'new value' I'm guessing

Re: loop over list and modify in place

2006-09-30 Thread Daniel Nogradi
Call me crazy, but isn't the simple construct for obj in mylist: obj.newattribute = 'new value' what the OP was looking for? Yes, of course. That's why my follow-up post was this: Please consider the previous question as an arbitrary random brain cell fluctuation whose

Re: loop over list and modify in place

2006-09-30 Thread Paul Rubin
Daniel Nogradi [EMAIL PROTECTED] writes: Is looping over a list of objects and modifying (adding an attribute to) each item only possible like this? mylist = [ obj1, obj2, obj3 ] for i in xrange( len( mylist ) ): mylist[i].newattribute = 'new value' for m in mylist: m.newattribute

Re: convert loop to list comprehension

2006-09-09 Thread Simon Forman
[EMAIL PROTECTED] wrote: snip Thanks for that, Carl. I think that using the loop is probably what I'll end up doing. I had no idea that the listcomp thing would be quite a complicated as it is appearing. I had it in my mind that I was missing some obvious thing which would create a simple

Re: convert loop to list comprehension

2006-09-09 Thread Steve Holden
[EMAIL PROTECTED] wrote: [...] Cool ... and damn but you guys are fast with the answers. This appears to work find, but in a quick and dirty test it appears that the [list] version takes about 2x as long to run as the original loop. Is this normal? No hard and fast information, but as a

Re: convert loop to list comprehension

2006-09-09 Thread Steve Holden
[EMAIL PROTECTED] wrote: Carl Banks wrote: [EMAIL PROTECTED] wrote: Paul Rubin wrote: [EMAIL PROTECTED] writes: print sum( ([i]*n for i,n in enumerate(seq)), []) Wow, I had no idea you could do that. After all the discussion about summing strings, I'm astonished. Me too ... despite what

Re: convert loop to list comprehension

2006-09-09 Thread [EMAIL PROTECTED]
Paul Rubin wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: FWIW, the original loop looked perfectly fine and readable and I'd suggest going with that over these hacked-up listcomp solutions. Don't use a listcomp just for the sake of using a listcomp. Thanks for that, Carl. I

convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
Please help my poor brain :) Every time I try to do a list comprehension I find I just don't comprehend ... Anyway, I have the following bit of code: seq = [2, 3, 1, 9] tmp = [] for a in range(len(seq)): tmp.extend([a]*seq[a]) which correctly returns: [0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3,

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
[EMAIL PROTECTED] [EMAIL PROTECTED] writes: seq = [2, 3, 1, 9] tmp = [] for a in range(len(seq)): tmp.extend([a]*seq[a]) which correctly returns: [0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3] Question is, can I do this as a list comprehension? import operator x =

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
Paul Rubin http://[EMAIL PROTECTED] writes: Question is, can I do this as a list comprehension? import operator x = reduce(operator.add, ([i]*a for i,a in enumerate(seq)), []) Maybe more in the iterative spirit: import itertools seq = [2, 3, 1, 9] x = itertools.chain(*([i]*a for i,a in

Re: convert loop to list comprehension

2006-09-08 Thread Rob Williscroft
[EMAIL PROTECTED] wrote in news:1157758817.446690.105620 @i42g2000cwa.googlegroups.com in comp.lang.python: Please help my poor brain :) Every time I try to do a list comprehension I find I just don't comprehend ... Anyway, I have the following bit of code: seq = [2, 3, 1, 9] tmp = []

Re: convert loop to list comprehension

2006-09-08 Thread bearophileHUGS
Two possibile solutions: seq = [2, 3, 1, 9] print sum( ([i]*n for i,n in enumerate(seq)), []) print [i for i, x in enumerate(seq) for _ in xrange(x)] The second one is probably quite faster. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
Rob Williscroft wrote: snip But my forth attemp yeilded (If that's a pun I do appologise) this: [ x for a in range(len(seq)) for x in [a] * seq[a] ] Ahh, that's the magic ... I didn't understand that one could have multiple statments in this single line. Now, you can't have python line for

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
[EMAIL PROTECTED] writes: print sum( ([i]*n for i,n in enumerate(seq)), []) Wow, I had no idea you could do that. After all the discussion about summing strings, I'm astonished. -- http://mail.python.org/mailman/listinfo/python-list

Re: convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote: Two possibile solutions: seq = [2, 3, 1, 9] print sum( ([i]*n for i,n in enumerate(seq)), []) print [i for i, x in enumerate(seq) for _ in xrange(x)] Cool as well. So much to learn :) 1. Using an _ is an interesting way to use a throw-away variable. Never would

Re: convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
Paul Rubin wrote: [EMAIL PROTECTED] writes: print sum( ([i]*n for i,n in enumerate(seq)), []) Wow, I had no idea you could do that. After all the discussion about summing strings, I'm astonished. Me too ... despite what bearophile said, this is faster than the 2nd example. Nearly as fast

Re: convert loop to list comprehension

2006-09-08 Thread Felipe Almeida Lessa
8 Sep 2006 17:37:02 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED]: 1. Using an _ is an interesting way to use a throw-away variable. Never would I think of that ... but, then, I don't do Perl either :) It's a kind of convention. For example, Pylint complains for all variables you set and don't use

Re: convert loop to list comprehension

2006-09-08 Thread Felipe Almeida Lessa
08 Sep 2006 17:33:20 -0700, Paul Rubin http://phr.cx@nospam.invalid: [EMAIL PROTECTED] writes: print sum( ([i]*n for i,n in enumerate(seq)), []) Wow, I had no idea you could do that. After all the discussion about summing strings, I'm astonished. Why? You already had the answer: summing

Re: convert loop to list comprehension

2006-09-08 Thread MonkeeSage
[EMAIL PROTECTED] wrote: Cool ... and damn but you guys are fast with the answers. This appears to work find, but in a quick and dirty test it appears that the [list] version takes about 2x as long to run as the original loop. Is this normal? You could also do it 'functionally' with map(),

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
MonkeeSage [EMAIL PROTECTED] writes: Ps. I don't know if xrange is faster...I thought the difference was that range created a temporary variable holding a range object and xrange created an iterator? There's no such thing as a range object; range creates a list, which consumes O(n) memory

Re: convert loop to list comprehension

2006-09-08 Thread MonkeeSage
Paul Rubin wrote: MonkeeSage [EMAIL PROTECTED] writes: Ps. I don't know if xrange is faster...I thought the difference was that range created a temporary variable holding a range object and xrange created an iterator? There's no such thing as a range object; range creates a list, which

Re: convert loop to list comprehension

2006-09-08 Thread Carl Banks
[EMAIL PROTECTED] wrote: Paul Rubin wrote: [EMAIL PROTECTED] writes: print sum( ([i]*n for i,n in enumerate(seq)), []) Wow, I had no idea you could do that. After all the discussion about summing strings, I'm astonished. Me too ... despite what bearophile said, this is faster than

Re: convert loop to list comprehension

2006-09-08 Thread [EMAIL PROTECTED]
Carl Banks wrote: [EMAIL PROTECTED] wrote: Paul Rubin wrote: [EMAIL PROTECTED] writes: print sum( ([i]*n for i,n in enumerate(seq)), []) Wow, I had no idea you could do that. After all the discussion about summing strings, I'm astonished. Me too ... despite what bearophile

Re: convert loop to list comprehension

2006-09-08 Thread Paul Rubin
[EMAIL PROTECTED] [EMAIL PROTECTED] writes: FWIW, the original loop looked perfectly fine and readable and I'd suggest going with that over these hacked-up listcomp solutions. Don't use a listcomp just for the sake of using a listcomp. Thanks for that, Carl. I think that using the loop

Re: For loop and list comprehension similarity

2006-03-27 Thread s . lipnevich
I think I like generator comprehension in this case better than either list comprehension or a filter because both of the latter create a new full result list before the loop even begins. At least I suppose they do. Also, I think Mitja's suggestion if not test: continue and Terry's filter function

Re: For loop and list comprehension similarity

2006-03-27 Thread Peter Hansen
[EMAIL PROTECTED] wrote: Do you think this discussion is a proof that the following principle got violated, or do you think that loop with condition is not such an atomic thing to be subject to this: There should be one -- and preferably only one -- obvious way to do it. Mitja's suggestion

Re: For loop and list comprehension similarity

2006-03-27 Thread Terry Reedy
Peter Hansen [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Do you think this discussion is a proof that the following principle got violated, or do you think that loop with condition is not such an atomic thing to be subject to this: There should be one

For loop and list comprehension similarity

2006-03-26 Thread s . lipnevich
Hi All, I apologize if this was brought up before, I couldn't find any prior art :-). On more than one occasion, I found myself wanting to use a conditional loop like this (with Invalid syntax error, of course): for i in c if test: print i*2 ...because it's similar to

Re: For loop and list comprehension similarity

2006-03-26 Thread Mitja Trampus
[EMAIL PROTECTED] wrote: On more than one occasion, I found myself wanting to use a conditional loop like this (with Invalid syntax error, of course): for i in c if test: print i*2 Maybe there's been a PEP, don't really know... Currently, the only sensible alternative

Re: For loop and list comprehension similarity

2006-03-26 Thread s . lipnevich
Thank you for replying, Mitja! That *is* a nice alternative. Do you think it's a good idea to ask on comp.python.devel if they would be interested in a PEP about this (provided there is none)? Cheers, Sergey. -- http://mail.python.org/mailman/listinfo/python-list

Re: For loop and list comprehension similarity

2006-03-26 Thread Grant Edwards
On 2006-03-26, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi All, I apologize if this was brought up before, I couldn't find any prior art :-). On more than one occasion, I found myself wanting to use a conditional loop like this (with Invalid syntax error, of course): for i in c if

Re: For loop and list comprehension similarity

2006-03-26 Thread Ben Finney
[EMAIL PROTECTED] writes: On more than one occasion, I found myself wanting to use a conditional loop like this (with Invalid syntax error, of course): for i in c if test: print i*2 ...because it's similar to the list comprehension construct: [i*2 for i in c if

Re: For loop and list comprehension similarity

2006-03-26 Thread s . lipnevich
Why not combine the two: I guess because (at least in source code) you're doing a loop twice :-). I don't know what a compiler would do. I think though that the for i in c if test: construct is more readable and maybe can even be better optimized. Thanks! Sergey. --

Re: For loop and list comprehension similarity

2006-03-26 Thread John Zenger
Rather than a list comprehension, it would be faster and more memory-efficient to use a generator comprehension. Just change the square brackets to parentheses: for j in (i*2 for i in c if test): print j Grant Edwards wrote: On 2006-03-26, [EMAIL PROTECTED] [EMAIL PROTECTED]

Re: For loop and list comprehension similarity

2006-03-26 Thread Terry Reedy
[EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Why not combine the two: I guess because (at least in source code) you're doing a loop twice :-). I don't know what a compiler would do. I think though that the for i in c if test: construct is more readable and maybe can even be

change vars in place w/loop or list comprehension

2005-05-31 Thread ken . boss
I am a python newbie, and am grappling with a fundamental concept. I want to modify a bunch of variables in place. Consider the following: a = 'one' b = 'two' c = 'three' list = [a, b, c] for i in range(len(list)): ... list[i] = list[i].upper() ... [a, b, c] = list a 'ONE' or, better:

Re: change vars in place w/loop or list comprehension

2005-05-31 Thread Jatinder Singh
Hi what u can do over here is add a,b,c... in a list e.g. list.append(vars..) and then use the statement newlist = map(lambda x:x.upper(),list) Now ur newlist will contain the modified list. HOPE THIS THE BETTER SOLUTION TO UR PROBLEM Quoting [EMAIL PROTECTED]: I am a python newbie, and am

Re: change vars in place w/loop or list comprehension

2005-05-31 Thread Raymond Hettinger
a = 'one' b = 'two' c = 'three' [a, b, c] = [s.upper() for s in [a, b, c]] a 'ONE' Both of these accomplish what I'm after; I prefer the second for its brevity. But either approach requires that I spell out my list of vars-to-alter [a, b, c] twice. This strikes me as awkward, and

Re: change vars in place w/loop or list comprehension

2005-05-31 Thread Peter Hansen
[EMAIL PROTECTED] wrote: I am a python newbie, and am grappling with a fundamental concept. I want to modify a bunch of variables in place. Consider the following: [snip] a = 'one' b = 'two' c = 'three' [a, b, c] = [s.upper() for s in [a, b, c]] Both of these accomplish what I'm after; I

Re: Loop in list.

2005-02-10 Thread beliavsky
Jim wrote: Wow! All I wanted to do was write the equivalence of the Fortran statement: Real*4 matrix(n,n). If you are doing numerical linear algebra in Python, you should use the Numeric or Numarray modules. With Numeric, the equivalent is just from Numeric import zeros matrix =

Re: Loop in list.

2005-02-10 Thread Jim
I assume this is one of the addons for Python. I know that there is a great deal of stuff out there available for Python that does some of the stuff that I am looking at, but I am interested in learning to use Python. When I want to get faster and more general, I will get some of this stuff or

Re: Loop in list.

2005-02-09 Thread Jim
Wow! All I wanted to do was write the equivalence of the Fortran statement: Real*4 matrix(n,n). I'm going to have to go to the intrepreter to see what your saying. Thanks for all the help. Jim -- http://mail.python.org/mailman/listinfo/python-list

Loop in list.

2005-02-08 Thread Jim
Where did this type of structure come from: mat = ['a' for i in range(3)]? This will produce a list of three elements but I don't see reference for it in any of the books. -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop in list.

2005-02-08 Thread Jeremy Jones
Jim wrote: Where did this type of structure come from: mat = ['a' for i in range(3)]? This will produce a list of three elements but I don't see reference for it in any of the books. It's called a list comprehension and it appeared in Python 2.0.

Re: Loop in list.

2005-02-08 Thread Fredrik Lundh
Jim [EMAIL PROTECTED] wrote: Where did this type of structure come from: mat = ['a' for i in range(3)]? This will produce a list of three elements but I don't see reference for it in any of the books. it's called list comprehension, and was added in Python 2.0.

Re: Loop in list.

2005-02-08 Thread Diez B. Roggisch
Jim wrote: Where did this type of structure come from: mat = ['a' for i in range(3)]? This will produce a list of three elements but I don't see reference for it in any of the books. Its called a list-comprehension. And as I don't know what books you mean, I can't say if its covered

Re: Loop in list.

2005-02-08 Thread Bill Mill
Jim, That is called a list comprehension, and it is a feature which appeared in python 2.3 (iirc). Thus if your books are about earlier versions of python, list comprehensions will not be covered. Check out the section of the tutorial about them at

Re: Loop in list.

2005-02-08 Thread Simon Brunning
On Tue, 08 Feb 2005 06:50:31 -0800 (PST), Jim [EMAIL PROTECTED] wrote: Where did this type of structure come from: mat = ['a' for i in range(3)]? This will produce a list of three elements but I don't see reference for it in any of the books. It's called a List Comprehension. There's

Re: Loop in list.

2005-02-08 Thread Roy Smith
Jim [EMAIL PROTECTED] wrote: Where did this type of structure come from: mat = ['a' for i in range(3)]? This will produce a list of three elements but I don't see reference for it in any of the books. It's a list comprehension. Unfortunately, this is a bad example of one, since a much

Re: Loop in list.

2005-02-08 Thread Jim
Thanks for the help. Python is somewhat mysterious to an old fortan programer. Jim -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop in list.

2005-02-08 Thread Jim
Particularly one who can't spell. Fortran. Jim -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop in list.

2005-02-08 Thread Caleb Hattingh
Jim Someone on this list (SteveB) helped me quite a bit with a list comprehension on a recent thread. Roy said it can be hard to read, and I agree in part because I always thought they were hard to read, when in actual fact I had just never bothered to learn properly. Here is a

Re: Loop in list.

2005-02-08 Thread Stephen Thorne
On Tue, 08 Feb 2005 23:07:09 -0500, Caleb Hattingh [EMAIL PROTECTED] wrote: ' a = [i*2*b for i in range(3) for b in range(4)] ' a [0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12] Might take you a while to correlate the answer with the loop, but you should be able to see after a while that this nesting

Re: Loop in list.

2005-02-08 Thread Bruno Desthuilliers
Jim a écrit : Where did this type of structure come from: mat = ['a' for i in range(3)]? This will produce a list of three elements but I don't see reference for it in any of the books. Now everyone told you *what* is it, I'll (very very dumbly) answer the question : this syntax comes from

Re: Loop in list.

2005-02-08 Thread Caleb Hattingh
Stephen You're gonna have to help me here.what is the effective difference? Thanks Caleb ' a = [] ' for b in range(4): ' for i in range(3): ' a.append(i*2*b) There is a subtle error in this explanation. The equivilence actually looks like: ' a = [] ' l1 = range(4) ' l2 = range(3) '

Re: Loop in list.

2005-02-08 Thread Fredrik Lundh
Stephen Thorne wrote: ' a = [i*2*b for i in range(3) for b in range(4)] ' a [0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12] Might take you a while to correlate the answer with the loop, but you should be able to see after a while that this nesting is the same as ' a = [] ' for b in range(4): '

Re: Loop in list.

2005-02-08 Thread Roy Smith
Jim [EMAIL PROTECTED] wrote: Thanks for the help. Python is somewhat mysterious to an old fortan programer. You might appreciate http://www.python.org/doc/Humor.html#habits -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop in list.

2005-02-08 Thread Caleb Hattingh
Hi Fredrik *sigh* I think I will stop writing mini-tutorials :) You are, of course, correct. And I really like your method of explaining how to mentally juggle the LC into explicit loops. I shudder to think how mnay people I confused with my incorrect examples - I really should have tested