473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

for(each) element aliasing


I know how to alter the list via list indicies but I would like to know if
there is posibility to write in Python something like:

my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";

and get the result
-------
2 4 6
--
Matija
Jul 18 '05 #1
8 1530
Aloha,

Matija Papec wrote:
my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";
2 4 6

arr = [1,2,3]
print [element*2 for element in arr]

[2, 4, 6]

Wishing a happy day
LOBI
Jul 18 '05 #2
On Mon, 22 Nov 2004 17:21:26 +0100, Matija Papec <mp****@yahoo.com> wrote:

I know how to alter the list via list indicies but I would like to know if
there is posibility to write in Python something like:

my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";
For instance :
arr = 1, 2, 3 # range(1, 4)
arr = [ element*2 for element in arr ]
print arr # [2, 4, 6] it is a list
for element in arr:
print element, # note the comma
print # useful if not in interactive mode only
Read the documentation about "list comprehension"
and get the result
-------
2 4 6

Jul 18 '05 #3
Matija Papec wrote:
I know how to alter the list via list indicies but I would like to know if
there is posibility to write in Python something like:

my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";

and get the result
-------
2 4 6


Something like this?

arr= range(1,4)
for index, value in enumerate( arr):
arr[index]*= 2

print arr

Regards,

Will McGugan
Jul 18 '05 #4
Will McGugan wrote:
Matija Papec wrote:
I know how to alter the list via list indicies but I would like to know if
there is posibility to write in Python something like:

my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";

and get the result
-------
2 4 6


Something like this?

arr= range(1,4)
for index, value in enumerate( arr):
arr[index]*= 2


But for this you won't need enumerate:

One of
for index in range(len(arr)):
for index in xrange(len(arr)):

is sufficient.

Reinhold

--
[Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer
jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen
jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs-
mitteln inkompatibel. -- Florian Diesch in dcoulm
Jul 18 '05 #5
Reinhold Birkenfeld wrote:
But for this you won't need enumerate:
for index in range(len(arr)):


Isn't the range(len(arr)) construct somewhat of an
contraption? It has become a recognizable pattern
of course but that was by necessity rather than
correctness.

Istvan.
Jul 18 '05 #6
X-Ftn-To: Jp Calderone

Jp Calderone <ex*****@divmod.com> wrote:
-------
2 4 6


No. Integers in Python are immutable. For lists of other things, mutation in-place (similar to the above example) is possible, but for integers and other immutable types, it is not. You may be interested in list comprehensions, though:

arr = range(1, 4)
arr[:] = [a * 2 for a in arr]
print arr


I guess this works like "map" but "for" is more idimatic.
Thank you all for so quick responses. :)
--
Matija
Jul 18 '05 #7
Matija Papec wrote:
X-Ftn-To: Jp Calderone

Jp Calderone <ex*****@divmod.com> wrote:
-------
2 4 6


No. Integers in Python are immutable. For lists of other things, mutation in-place (similar to the above example) is possible, but for integers and other immutable types, it is not. You may be interested in list comprehensions, though:

arr = range(1, 4)
arr[:] = [a * 2 for a in arr]
print arr


I guess this works like "map" but "for" is more idimatic.


Yes. List comprehensions are elegant (and fast) expressions you would
otherwise construct with map() and filter().

Reinhold

--
[Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer
jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen
jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs-
mitteln inkompatibel. -- Florian Diesch in dcoulm
Jul 18 '05 #8
>> arr[:] = [a * 2 for a in arr]
print arr


I guess this works like "map" but "for" is more idimatic.


idiomatic
--
Matija
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Petterson Mikael | last post by:
Hi, I have one xml file with many class elements. For each class element I need to generate a new new java file.As it is now I get ( my output ) all classes in one file. Do I control this in the...
8
by: Mark Constant | last post by:
I have a xslt file and it keeps giving me an EOF error when it reaches the point <xsl:for-each select="lc:Entertainment/lc:$Hardware"> and <xsl:for-each select="lc:Entertainment/lc:$Hardware"> ...
2
by: John Estess | last post by:
The following xml file is converted by the piece of xml below (oil.xsl). I have the right number of dots for the unordered list items, but it won't print out the "use's". What am I doing wrong? ...
2
by: martin.tschofen | last post by:
How do I select only the first node from a for-each loop that contains a the element "photo". the following xsl finds all "art" elements that have a "photo" element. The problem is that I only...
5
by: hiroshi ochi | last post by:
Hello, Using MSIE 6.0 and above, with javascript is it possible to display an individual tooltip for each item in a listbox? I need this functionality to show the listitems that are longer...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
4
by: Patrick | last post by:
Hello All, I am new to xml and trying to make it work. I'm having a bit of trouble with for-each loops. I have this xml document; <http://ocgserv.marine.usf.edu/patrick/xml/test_xml_copy.xml>...
5
by: IanT | last post by:
Public Class MyForEachBug ' Public Class MyData Public Element As String Public Group() As String = {"a", "b", "c"} End Class ' Public Sub New() ' Dim Element As String
1
by: gah | last post by:
I am getting an array of records from an asp.net webservice and wanting to display them in a dynamically created html table in the php page. I am new to php and not sure how to get the values from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.