On 09/27/2016 09:20 PM, Steven D'Aprano wrote:
On Wednesday 28 September 2016 12:48, Larry Hudson wrote:
As they came through in the newsgroup, BOTH run correctly, because both
versions had leading spaces only.
(I did a careful copy/paste to check this.)
Copying and pasting from the news clie
On Wednesday 28 September 2016 12:48, Larry Hudson wrote:
> As they came through in the newsgroup, BOTH run correctly, because both
> versions had leading spaces only.
> (I did a careful copy/paste to check this.)
Copying and pasting from the news client may not be sufficient to show what
whites
On 09/26/2016 01:57 PM, Cai Gengyang wrote:
Ok it works now:
for row in range(10):
for column in range(10):
print("*",end="")
but how is it different from ---
for row in range
Cai Gengyang wrote:
How are you running the interactive interpreter? Are you
using IDLE, or are you running Python in a command window? --- IDLE
I don't normally use IDLE on MacOSX, so I had to try it to
find out. I think I know what your problem is now.
When you type a line into IDLE ending w
On Tuesday, September 27, 2016 at 2:14:05 PM UTC+8, Cai Gengyang wrote:
> We're trying to help, but we need to know more about the
> environment you're using to enter your code.
>
> What operating system are you using? --- OSX Yosemite Version 10.10.2
>
> How are you running the interactive int
We're trying to help, but we need to know more about the
environment you're using to enter your code.
What operating system are you using? --- OSX Yosemite Version 10.10.2
How are you running the interactive interpreter? Are you
using IDLE, or are you running Python in a command window? --- ID
Cai Gengyang wrote:
I'll still be asking for help here. Please help out a newbie.
We're trying to help, but we need to know more about the
environment you're using to enter your code.
What operating system are you using?
How are you running the interactive interpreter? Are you
using IDLE, or
On Monday, September 26, 2016 at 9:57:52 PM UTC+1, Cai Gengyang wrote:
> Ok it works now:
>
> >>>for row in range(10):
> for column in range(10):
>print("*",end="")
>
>
>
>
>
Sure, I just sent in a subscription request to it ... but I'll still be asking
for help here. Please help out a newbie. When I master this language I can help
other new users too (This is good for the world and for everyone involved).
Ideally, Information and education should be free and not loc
Cai Gengyang :
> What is a tab and what is a space in python and what's the difference
> ?
>
> Which piece of code is indented with tabs and which one is indented
> with spaces ?
Key questions that Python gurus are having a hard time answering!
Equally confusing, you might run into this phantom p
Ok it works now:
>>>for row in range(10):
for column in range(10):
print("*",end="")
but how is it different from ---
>>> for row in range(10):
for column in range(10)
On 9/26/2016 12:54 PM, Cai Gengyang wrote:
Which piece of code is indented with tabs and which one is indented with spaces
?
I told you in my initial answer, where I said, referring to the two
indented lines in one 'piece of code', "These indents are 4 spaces and 1
tabs." It is the mixture
On 09/26/2016 08:25 AM, Cai Gengyang wrote:
I just wanted to note that sometimes the code works, sometimes it doesn't.
(even though both are exactly the same code) ... Weird , dum dum dum
It is NOT weird. Python is being consistent, YOU are not.
These examples are NOT "exactly the same code
Cai Gengyang writes:
> What is a tab and what is a space in python and what's the difference
> ?
Try print('x\tx') in Python to see a tab character between the two x's.
For me it looks the same as seven spaces, for you it will also look like
some amount of whitespace but it might be a different a
On 09/26/2016 06:54 PM, Cai Gengyang wrote:
What is a tab and what is a space in python and what's the difference ?
Which piece of code is indented with tabs and which one is indented with spaces
?
Please do not top-post in this list. Put your text after the message you
quote.
Tabs and sp
On Tue, 27 Sep 2016 01:25 am, Cai Gengyang wrote:
> I just wanted to note that sometimes the code works, sometimes it doesn't.
> (even though both are exactly the same code) ... Weird , dum dum dum
They are not the same code. One of them mixes tabs and spaces for the same
indent level, the other
What is a tab and what is a space in python and what's the difference ?
Which piece of code is indented with tabs and which one is indented with spaces
?
On Tuesday, September 27, 2016 at 12:40:16 AM UTC+8, MRAB wrote:
> On 2016-09-26 16:25, Cai Gengyang wrote:
> > I just wanted to note that
On 2016-09-26 16:25, Cai Gengyang wrote:
I just wanted to note that sometimes the code works, sometimes it doesn't.
(even though both are exactly the same code) ... Weird , dum dum dum
for row in range(10):
for column in range(10):
print("*",end="")
SyntaxError: inco
On 09/26/2016 05:25 PM, Cai Gengyang wrote:
I just wanted to note that sometimes the code works, sometimes it doesn't.
(even though both are exactly the same code) ... Weird , dum dum dum
for row in range(10):
for column in range(10):
print("*",end="")
SyntaxError:
I just wanted to note that sometimes the code works, sometimes it doesn't.
(even though both are exactly the same code) ... Weird , dum dum dum
>>> for row in range(10):
for column in range(10):
print("*",end="")
SyntaxError: inconsistent use of tabs and spaces in inde
On Monday 26 September 2016 18:32, Cai Gengyang wrote:
> These are my attempts ---
That's nice. Do you have a question?
> SyntaxError: inconsistent use of tabs and spaces in indentation
When you indent, press TAB or SPACE but not both.
This error can only happen if you are use spaces for some
Cai Gengyang writes:
> These are my attempts ---
>
for row in range(10):
> for column in range(10):
> print("*",end=" ")
>
> SyntaxError: inconsistent use of tabs and spaces in indentation
What do you type yourself? Could it be that your software starts the
second line with
Steven D'Aprano writes:
> P.S. Hey Jussi, is the backspace key on your keyboard broken? Every
> time somebody bottom-posts without trimming, a pixie dies...
I was annoyed by the top-posted one-liner in response to the last line
of Terry's response. I responded in kind and then it was too late. I'
These are my attempts ---
>>> for row in range(10):
for column in range(10):
print("*",end=" ")
SyntaxError: inconsistent use of tabs and spaces in indentation
>>> for row in range(10):
for column in range(10):
print("*",end=" ")
SyntaxError: expected an indented
On Monday 26 September 2016 17:21, Jussi Piitulainen wrote:
> Cai Gengyang writes:
[snip 80 or so lines]
> Reindent your lines.
In case Cai doesn't know what "reindent" means:
It depends on your text editor. At worst, you have to delete all the indents,
and re-enter them, using ONLY spaces, o
Cai Gengyang writes:
> So what do i need to do to correct the error ?
>
> Regards
>
>
> On Monday, September 26, 2016 at 2:48:16 PM UTC+8, Terry Reedy wrote:
>> On 9/26/2016 1:59 AM, Cai Gengyang wrote:
>> > Why is it that you need a print() at the end to create the table for
>> > example 1:
>>
So what do i need to do to correct the error ?
Regards
On Monday, September 26, 2016 at 2:48:16 PM UTC+8, Terry Reedy wrote:
> On 9/26/2016 1:59 AM, Cai Gengyang wrote:
> > Why is it that you need a print() at the end to create the table for
> > example 1:
> >
> > Example 1 ---
> >
> for r
On 9/26/2016 1:59 AM, Cai Gengyang wrote:
Why is it that you need a print() at the end to create the table for example 1:
Example 1 ---
for row in range(10):
for column in range(10):
print("*",end=" ")
# Print a blank line for next row
print()
These indents are either 4
Cai Gengyang writes:
> Why is it that you need a print() at the end to create the table for
> example 1:
>
> Example 1 ---
>
for row in range(10):
> for column in range(10):
> print("*",end=" ")
>
> # Print a blank line for next row
> print()
[- -]
> When I try to do e
Why is it that you need a print() at the end to create the table for example 1:
Example 1 ---
>>> for row in range(10):
for column in range(10):
print("*",end=" ")
# Print a blank line for next row
print()
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
Sven R. Kunze wrote:
> On 16.03.2016 16:02, Tim Chase wrote:
>> On 2016-03-16 15:29, Sven R. Kunze wrote:
>>> I would re-use the "for-else" for this. Everything I thought I
>>> could make use of the "-else" clause, I was disappointed I couldn't.
>> Hmm...this must be a mind-set thing. I use the "
Peter Otten schreef op 2016-03-16 13:57:
If you don't like exceptions implement (or find) something like
items = peek(items)
if items.has_more():
# at least one item
for item in items:
...
else:
# empty
Only if such a function is used a lot or cannot be conceived without
severe
On Sat, Mar 19, 2016 at 7:49 AM, Sven R. Kunze wrote:
> On 18.03.2016 20:10, Palpandi wrote:
>>
>> You can do like this.
>>
>> if not my_iterable:
>>
>> for x in my_iterable:
>>
>
>
> Thanks for you help here, however as already pointed out, my_iterable is not
> necessarily a list but
alister wrote:
> On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
>> I'm kidding, of course. Keep it simple and use a flag like you would in
>> any other language:
>>
>> empty = True:
>> for item in items:
>> empty = False ...
>> if empty:
>> ...
>
> or even use the loop variabl
On Wed, Mar 16, 2016, at 13:01, Sven R. Kunze wrote:
> On 16.03.2016 17:56, Sven R. Kunze wrote:
> > On 16.03.2016 17:37, Random832 wrote:
> >> for item in collection:
> >> if good(item):
> >>thing = item
> >>break
> >> else:
> >> thing = default # or raise an exception, etc
On 16.03.2016 16:02, Tim Chase wrote:
On 2016-03-16 15:29, Sven R. Kunze wrote:
I would re-use the "for-else" for this. Everything I thought I
could make use of the "-else" clause, I was disappointed I couldn't.
Hmm...this must be a mind-set thing. I use the "else" clause with
for/while loops
On 16.03.2016 18:08, Random832 wrote:
Yeah, well, you can *almost* get there with:
try:
thing = next(item for item in collection if good(item))
except StopIteration:
thing = default
But the for/else thing seems like a more natural way to do it. Plus,
this is a toy example, if the body
On 2016-03-16 11:23, Sven R. Kunze wrote:
> for x in my_iterable:
> # do
> empty:
> # do something else
>
> What's the most Pythonic way of doing this?
If you can len() on it, then the obvious way is
if my_iterable:
for x in my_iterable:
do_something(x)
else:
somethin
On 16.03.2016 11:47, Peter Otten wrote:
What would you expect?
A keyword filling the missing functionality? Some Python magic, I
haven't seen before. ;-)
class Empty(Exception): pass
...
def check_empty(items):
... items = iter(items)
... try:
... yield next(items)
...
On 18.03.2016 20:10, Palpandi wrote:
You can do like this.
if not my_iterable:
for x in my_iterable:
Thanks for you help here, however as already pointed out, my_iterable is
not necessarily a list but more likely an exhaustible iterator/generator.
Best,
Sven
--
https://mail.pyth
André Roberge wrote:
> On Wednesday, 16 March 2016 07:23:48 UTC-3, Sven R. Kunze wrote:
>> Hi,
>>
>> a colleague of mine (I write this mail because I am on the list) has the
>> following issue:
>>
>>
>> for x in my_iterable:
>> # do
>> empty:
>> # do something else
>>
>>
>> What's
ke use of the "-else" clause, I was disappointed I couldn't.
I find the addition to for-loop as useful as we already have a quite
complex try-except-else-finally clause. I don't know why for-loops
couldn't benefit from this as well.
Best,
Sven
--
https://mail.python.org/mailman/listinfo/python-list
Tim Chase wrote:
> On 2016-03-16 16:53, Peter Otten wrote:
>> > item=None
>> > for item in items:
>> > #do stuff
>> if item is None:
>> > #do something else
>>
>> I like that better now I see it.
>
> The only problem with that is if your iterable returns None as the
> last item
Sven R. Kunze wrote:
> On 16.03.2016 11:47, Peter Otten wrote:
>>
>> What would you expect?
>
> A keyword filling the missing functionality? Some Python magic, I
> haven't seen before. ;-)
>
>>
> class Empty(Exception): pass
>> ...
> def check_empty(items):
>> ... items = iter(items)
On 2016-03-16 15:29, Sven R. Kunze wrote:
> I would re-use the "for-else" for this. Everything I thought I
> could make use of the "-else" clause, I was disappointed I couldn't.
Hmm...this must be a mind-set thing. I use the "else" clause with
for/while loops fairly regularly and would be miffed
On 16.03.2016 17:37, Random832 wrote:
On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote:
I can imagine that. Could you describe the general use-case? From what I
know, "else" is executed when you don't "break" the loop. When is this
useful?
for item in collection:
if good(item):
t
On 16.03.2016 13:08, Steven D'Aprano wrote:
Doing what? What is the code supposed to do? What's "empty" mean as a
keyword?
If you explain what your friends wants, then perhaps we can suggest
something. Otherwise we're just guessing. I can think of at least two
different meanings:
* run the "emp
On Wed, 16 Mar 2016 13:45:53 +, Mark Lawrence wrote:
> On 16/03/2016 13:25, alister wrote:
>> On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
>>
>>> Sven R. Kunze wrote:
>>>
Hi,
a colleague of mine (I write this mail because I am on the list) has
the following issue:
-else" for this. [Everything] I thought I could
make use of the "-else" clause, I was disappointed I couldn't.
[everytime]
I find the addition to for-loop as useful as we already have a quite
complex try-except-else-finally clause. I don't know why for-loops
couldn
On 16.03.2016 14:58, alister wrote:
no , i just typed it, while trying to hold a conversation with swmbo
:-( apologies to the op if e could not see where i was intending to go
with this.
No problem, I perform quite well at guessing folk's intention.
So, yes, I can extrapolate what you meant.
On 16.03.2016 14:09, Tim Chase wrote:
If you can len() on it, then the obvious way is
if my_iterable:
for x in my_iterable:
do_something(x)
else:
something_else()
However, based on your follow-up that it's an exhaustible iterator
rather than something you can len(), I'd u
On Wed, 16 Mar 2016 09:23 pm, Sven R. Kunze wrote:
> Hi,
>
> a colleague of mine (I write this mail because I am on the list) has the
> following issue:
>
>
> for x in my_iterable:
> # do
> empty:
> # do something else
>
>
> What's the most Pythonic way of doing this?
Doing what?
On Wednesday, 16 March 2016 07:23:48 UTC-3, Sven R. Kunze wrote:
> Hi,
>
> a colleague of mine (I write this mail because I am on the list) has the
> following issue:
>
>
> for x in my_iterable:
> # do
> empty:
> # do something else
>
>
> What's the most Pythonic way of doing this?
On Wed, 16 Mar 2016 11:41 pm, André Roberge wrote:
> for x in my_iterable:
># do something
>
> if not my_iterable:
># do something else
Doesn't work for iterators. Iterators are (in general) always truthy,
whether they are empty or not.
--
Steven
--
https://mail.python.org/mailman
On 3/16/2016 11:17 AM, Sven R. Kunze wrote:
On 16.03.2016 16:02, Tim Chase wrote:
Does it annoy me when I have to work in other languages that lack
Python's {for/while}/else functionality? You bet.
I can imagine that. Could you describe the general use-case? From what I
know, "else" is exec
On 2016-03-16 16:53, Peter Otten wrote:
> > item=None
> > for item in items:
> > #do stuff
> if item is None:
> > #do something else
>
> I like that better now I see it.
The only problem with that is if your iterable returns None as the
last item:
items = ["Something here", N
On 16/03/2016 13:25, alister wrote:
On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
Sven R. Kunze wrote:
Hi,
a colleague of mine (I write this mail because I am on the list) has
the following issue:
for x in my_iterable:
# do
empty:
# do something else
What's the most
On Wednesday, March 16, 2016 at 3:53:48 PM UTC+5:30, Sven R. Kunze wrote:
> Hi,
>
> a colleague of mine (I write this mail because I am on the list) has the
> following issue:
>
>
> for x in my_iterable:
> # do
> empty:
> # do something else
>
>
> What's the most Pythonic way of doi
On Thu, 17 Mar 2016 05:05 am, Sven R. Kunze wrote:
> What I don't understand is why Python features "if break, then no else
> clause", but "if empty, then empty clause".
>
> I found this excellent post:
> https://shahriar.svbtle.com/pythons-else-clause-in-loops
That post describes the motivating
On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
> Sven R. Kunze wrote:
>
>> Hi,
>>
>> a colleague of mine (I write this mail because I am on the list) has
>> the following issue:
>>
>>
>> for x in my_iterable:
>> # do
>> empty:
>> # do something else
>>
>>
>> What's the most
On 16.03.2016 17:20, Terry Reedy wrote:
On 3/16/2016 11:17 AM, Sven R. Kunze wrote:
On 16.03.2016 16:02, Tim Chase wrote:
Does it annoy me when I have to work in other languages that lack
Python's {for/while}/else functionality? You bet.
I can imagine that. Could you describe the general
On 16.03.2016 17:56, Sven R. Kunze wrote:
On 16.03.2016 17:37, Random832 wrote:
On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote:
I can imagine that. Could you describe the general use-case? From
what I
know, "else" is executed when you don't "break" the loop. When is this
useful?
for ite
On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote:
> I can imagine that. Could you describe the general use-case? From what I
> know, "else" is executed when you don't "break" the loop. When is this
> useful?
for item in collection:
if good(item):
thing = item
break
else:
th
On 17.03.2016 01:27, Steven D'Aprano wrote:
That post describes the motivating use-case for the introduction
of "if...else", and why break skips the "else" clause:
for x in data:
if meets_condition(x):
break
else:
# raise error or do additional processing
It might help to r
Sven R. Kunze wrote:
> Hi,
>
> a colleague of mine (I write this mail because I am on the list) has the
> following issue:
>
>
> for x in my_iterable:
> # do
> empty:
> # do something else
>
>
> What's the most Pythonic way of doing this?
What would you expect?
>>> class Empty(Exc
On 16.03.2016 11:28, Joaquin Alzola wrote:
If len(my_iterable) is not 0:
for x in my_iterable:
# do
else:
# do something else
I am sorry, I should have been more precise here.
my_iterable is an iterator that's exhausted after a complete iteration
and cannot be restored.
I
Behalf Of
Sven R. Kunze
Sent: 16 March 2016 10:23
To: Python List
Subject: empty clause of for loops
Hi,
a colleague of mine (I write this mail because I am on the list) has the
following issue:
for x in my_iterable:
# do
empty:
# do something else
What's the most Pythonic w
Hi,
a colleague of mine (I write this mail because I am on the list) has the
following issue:
for x in my_iterable:
# do
empty:
# do something else
What's the most Pythonic way of doing this?
Best,
Sven
--
https://mail.python.org/mailman/listinfo/python-list
On 04/23/2013 02:58 AM, inshu chauhan wrote:
Yes Simultaneously means all three running at the same time, I looked up
zip just now, but will it not disturb my dictionaries ?
And yes the dictionaries have same number of keys.
More crucially, do all the dictionaries have the *same* keys? If so,
inshu chauhan wrote:
> This statement is giving me the following error
>
> Statement:
> for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
> pixel_count.iterkeys())):
>
> Error:
> Traceback (most recent call last):
> File "C:\Users\inshu\Desktop\Training_segs_trial2.py", l
Am 23.04.2013 09:13, schrieb inshu chauhan:
This statement is giving me the following error
Statement:
for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
pixel_count.iterkeys())):
Error:
Traceback (most recent call last):
File "C:\Users\inshu\Desktop\Training_segs_trial2.
On Tue, Apr 23, 2013 at 5:13 PM, inshu chauhan wrote:
> This statement is giving me the following error
>
> Statement:
> for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
> pixel_count.iterkeys())):
You probably want to sort them separately. By the way, using
iterkeys() isn't
This statement is giving me the following error
Statement:
for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
pixel_count.iterkeys())):
Error:
Traceback (most recent call last):
File "C:\Users\inshu\Desktop\Training_segs_trial2.py", line 170, in
access_segments(segimage
Thanks Gary.
>
> Be clearer about the problem please.
>
> Do you wish to produce a loop that:
> On pass 1, each of p,k, and t hold the first item of their respective
> lists, and
> on pass 2, each of p,k, and t hold the second item of their respective
> lists, and
> so on
> until one (or a
zip isn't doing the required
On Tue, Apr 23, 2013 at 12:28 PM, inshu chauhan wrote:
> Yes Simultaneously means all three running at the same time, I looked up
> zip just now, but will it not disturb my dictionaries ?
> And yes the dictionaries have same number of keys.
>
> thanks
>
>
> On Tue, A
On 04/22/2013 11:40 PM, inshu chauhan wrote:
i have to implement the below line in one of my code:
for p in sorted(segments.iterkeys()) and for k in
sorted(class_count.iterkeys()) and for j in
sorted(pixel_count.iterkeys()):
Its giving me a syntax error which is obvious, but how can I make
Yes Simultaneously means all three running at the same time, I looked up
zip just now, but will it not disturb my dictionaries ?
And yes the dictionaries have same number of keys.
thanks
On Tue, Apr 23, 2013 at 12:16 PM, Chris Angelico wrote:
> On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan
>
On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan wrote:
> i have to implement the below line in one of my code:
>
> for p in sorted(segments.iterkeys()) and for k in
> sorted(class_count.iterkeys()) and for j in sorted(pixel_count.iterkeys()):
>
> Its giving me a syntax error which is obvious, but
i have to implement the below line in one of my code:
for p in sorted(segments.iterkeys()) and for k in
sorted(class_count.iterkeys()) and for j in sorted(pixel_count.iterkeys()):
Its giving me a syntax error which is obvious, but how can I make all three
for loop run simultaneously or any other
On 12/1/11 4:53 AM, Mark wrote:
Hi there,
I'm a complete beginner to Python and, aside from HTML and CSS, to coding in
general. I've spent a few hours on it and think I understand most of the syntax.
However, I'm wondering a bit about For Loops. I know that the basic syntax
Mark writes:
> I'm a complete beginner to Python and, aside from HTML and CSS, to
> coding in general. I've spent a few hours on it and think I understand
> most of the syntax.
Welcome!
You should work your way through the Python tutorial, from beginning to
end http://docs.python.org/tutorial/>
Thanks a lot for the answers everyone, I really appreciate you getting back to
me so quickly. I think that I understand where I am with this now :)
--
http://mail.python.org/mailman/listinfo/python-list
wondering a bit about For Loops. I know that the basic
syntax for them is to define a list, and then to use something like:
for x in y
However, what does "for" and "in" mean in this context? Can anyone
help me to understand this? I know it's a really basic question, bu
On 01/12/2011 08:53, Mark wrote:
Hi there,
I'm a complete beginner to Python and, aside from HTML and CSS, to coding in
general. I've spent a few hours on it and think I understand most of the syntax.
However, I'm wondering a bit about For Loops. I know that the basic syntax
for x in y
However, what does "for" and "in" mean in this context?
It means basically the same as in Englsish
Does the following links answer the question ?
http://www.ibiblio.org/g2swap/byteofpython/read/for-loop.html
http://dsnra.jpl.nasa.gov/software/Python/diveintopython.pdf (page 58)
H
Hi there,
I'm a complete beginner to Python and, aside from HTML and CSS, to coding in
general. I've spent a few hours on it and think I understand most of the syntax.
However, I'm wondering a bit about For Loops. I know that the basic syntax for
them is to define a list,
On Wed, 17 Aug 2011 20:08:23 -0700 (PDT), Emily Anne Moravec wrote:
> I want to add 5 to each element of a list by using a for loop.
>
> Why doesn't this work?
>
> numbers = [1, 2, 3, 4, 5]
> for n in numbers:
> n = n + 5
> print numbers
Because integers are immutable. You cannot turn 1 into
On 08/18/2011 07:22 AM, Mark Niemczyk wrote:
Or, using list comprehension.
numbers = [1, 2, 3, 4, 5]
numbers = [n + 5 for n in numbers]
numbers
[6, 7, 8, 9, 10]
Or, if you want it in-place:
numbers[:] = [n+5 for n in numbers]
which makes a difference if you have another reference to numb
Or, using list comprehension.
>>> numbers = [1, 2, 3, 4, 5]
>>> numbers = [n + 5 for n in numbers]
>>> numbers
[6, 7, 8, 9, 10]
--
http://mail.python.org/mailman/listinfo/python-list
On Aug 18, 1:08 pm, Emily Anne Moravec wrote:
> I want to add 5 to each element of a list by using a for loop.
>
> Why doesn't this work?
>
> numbers = [1, 2, 3, 4, 5]
> for n in numbers:
> n = n + 5
> print numbers
As the for loop steps through numbers, it assigns each integer value
to the
>
> I want to add 5 to each element of a list by using a for loop.
>
> Why doesn't this work?
>
> numbers = [1, 2, 3, 4, 5]
> for n in numbers:
> n = n + 5
> print numbers
>
>
The n variable in the for loop refers to each value in the list, not the
reference to the slot that value is stored in.
I want to add 5 to each element of a list by using a for loop.
Why doesn't this work?
numbers = [1, 2, 3, 4, 5]
for n in numbers:
n = n + 5
print numbers
--
http://mail.python.org/mailman/listinfo/python-list
"Stefan Behnel" wrote in message
news:mailman.563.1283921317.29448.python-l...@python.org...
BartC, 08.09.2010 03:45:
Getting back to the OP's code again (trivial and pointless as it might
seem), I got these results:
C (gcc 3.4.5 -O3) 0.8 secs
C (DMC-o) 2.3 secs
C (lccwin32 -O) 2.9 secs
B
"Steven D'Aprano" wrote in message
news:4c878be5$0$3$c3e8...@news.astraweb.com...
On Tue, 07 Sep 2010 11:00:03 +0100, BartC wrote:
for i in xrange(1):
a = a + f(i)
With Python 3 and def f(x): return x+1, unrolling this loop 4x improved
speed by 15%; 4.00 minutes reduces to 3.
Steven D'Aprano writes:
>> With Python 3 and def f(x): return x+1, unrolling this loop 4x improved
>> speed by 15%; 4.00 minutes reduces to 3.30 minutes.
> I'm afraid that I can't replicate those figures. In my test, unrolling
> the loop causes a massive SLOWDOWN of 37%, not a speed up. Here is
On Tue, 07 Sep 2010 11:00:03 +0100, BartC wrote:
>> for i in xrange(1):
>>a = a + f(i)
>>
>> then unrolling the loop is even less useful. The overhead of the loop
>> itself is likely to be trivial compared to the cost of calling f() 100
>> million times -- the added complexity to shave
BartC wrote:
> So 'range' is just a class like any other. And that a class is something
> you can blithely copy from one variable to another. And whenever you see
> 'range' anywhere, you can't always be certain that someone hasn't done:
>
> range = 42
>
> at some point.
True. I read an explanati
BartC, 08.09.2010 03:45:
Getting back to the OP's code again (trivial and pointless as it might
seem), I got these results:
C (gcc 3.4.5 -O3) 0.8 secs
C (DMC-o) 2.3 secs
C (lccwin32 -O) 2.9 secs
[...]
I've seen LuaJIT in action. It's timing for this test is 1.5 secs:
forget being only 10x slower
cs
That's why I was questioning the latter's performance in for-loops. But now
that I know a bit more about Python (having dynamic everything) the figure
is not so surprising. However, it's still slow!
what you are after, look at LUA with its JIT, or scheme + stalin.
I've seen LuaJ
177.0 secs
That's why I was questioning the latter's performance in for-loops. But now
that I know a bit more about Python (having dynamic everything) the figure
is not so surprising. However, it's still slow!
what you are after, look at LUA with its JIT, or scheme + stalin.
I
1 - 100 of 333 matches
Mail list logo