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
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
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
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
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
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:
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
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')
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,
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
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
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
--
[ {'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'},
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',
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',
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':
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':
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'},
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
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
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
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
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
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
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
[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
[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
[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
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
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,
[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 =
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
[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 = []
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
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
[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
[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
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
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
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
[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(),
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
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
[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
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
[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
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
[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
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
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
[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
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
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
[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
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.
--
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]
[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
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:
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
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
[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
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 =
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
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
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
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.
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.
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
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
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
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
Thanks for the help. Python is somewhat mysterious to an old fortan
programer.
Jim
--
http://mail.python.org/mailman/listinfo/python-list
Particularly one who can't spell. Fortran.
Jim
--
http://mail.python.org/mailman/listinfo/python-list
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
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
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
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)
'
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):
'
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
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
80 matches
Mail list logo