The following code:
numbers = [1, 2, 3]
for value in numbers:
value *= 2
print numbers
results in the following output:
[1, 2, 3]
The intent of the code was to produce this output:
[2, 4, 6]
What is the reason for the output produced?
What code should be used to obtain the desired output? 4 1302 tr*********@verizon.net writes: The following code:
numbers = [1, 2, 3] for value in numbers: value *= 2 print numbers
results in the following output: [1, 2, 3]
The intent of the code was to produce this output: [2, 4, 6]
What is the reason for the output produced? What code should be used to obtain the desired output?
How about this?
numbers = [1, 2, 3]
print [x * 2 for x in numbers]
--
Björn Lindström <bk**@stp.lingfil.uu.se>
Student of computational linguistics, Uppsala University, Sweden tr*********@verizon.net said unto the world upon 29/12/05 10:43 AM: The following code:
numbers = [1, 2, 3] for value in numbers: value *= 2 print numbers
results in the following output: [1, 2, 3]
The intent of the code was to produce this output: [2, 4, 6]
What is the reason for the output produced? What code should be used to obtain the desired output?
value is bound in the for loop, but none of that affects the list to
which numbers is bound.
One way:
IDLE 1.1.2 numbers = [1, 2, 3] numbers = [x * 2 for x in numbers] print numbers
[2, 4, 6]
HTH,
Brian vdB
On Thu, 2005-12-29 at 11:43, tr*********@verizon.net wrote: The following code:
numbers = [1, 2, 3] for value in numbers: value *= 2 print numbers
results in the following output: [1, 2, 3]
The intent of the code was to produce this output: [2, 4, 6]
What is the reason for the output produced? What code should be used to obtain the desired output?
1) Read http://www.effbot.org/zone/python-objects.htm and reread it
until you understand why your code doesn't work.
2) Use a list comprehension:
numbers = [ value*2 for value in numbers ]
-Carsten
As others have already posted, changing the value of 'value' has
nothing to do with the list variable 'numbers'. To modify the list in
place, you need to access its members by index. The following code
does this:
numbers = [1,2,3]
for i in range(len(numbers)):
numbers[i] *= 2
print numbers
But this is how a C or Java programmer would approach this task, and is
not very Pythonic.
A bit better is to use enumerate (avoids the ugly range(len(blah))
looping):
numbers = [1,2,3]
for i,val in enumerate(numbers):
numbers[i] = val*2
This also modifies the list in place, so that if you are modifying a
very long (and I mean really quite long - long enough to question why
you are doing this in the first place) list, then this approach avoids
making two lists, one with the old value and one with the new.
But the most Pythonic is to use one of the list comprehension forms
already posted, such as:
numbers = [ x*2 for x in numbers ]
-- Paul This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dustin.getz |
last post by:
consider the following working loop where Packet is a subclass of
list, with Packet.insert(index, iterable) inserting each item in
iterable into Packet at consecutive indexes starting at index.
...
|
by: IUnknown |
last post by:
Ok, we are all aware of the situation where modifying the folder
structure (adding files, folders, deleting files, etc) will result in
ASP.NET triggering a recompilation/restart of the application....
|
by: John [H2O] |
last post by:
I would like to write a function to write variables to a file and modify a
few 'counters'. This is to replace multiple instances of identical code in a
module I am writing.
This is my approach:...
|
by: ismailc |
last post by:
Good day, I need help.
I would like to Update a Sharepoint List (Document Library) Folders using Web Services with Javascript.
I have searched on Goggle but nothing that updates a document...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |