[Tutor] Can you modify every nth item in a list with a single assignment?

Bob Gailer bgailer@alum.rpi.edu
Thu Jun 12 12:09:02 2003


--=======26455778=======
Content-Type: multipart/alternative; x-avg-checked=avg-ok-17912FEA; boundary="=====================_6362669==.ALT"


--=====================_6362669==.ALT
Content-Type: text/plain; x-avg-checked=avg-ok-17912FEA; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit

At 10:20 PM 6/11/2003 -0400, R. Alan Monroe wrote:

>I know I can modify every nth list item using a for loop over a
>range(start,end,n), but can it be done in a single assignment, without
>a loop?

No. Any operation on multiple elements of a list in any language is going 
to be implemented as a loop at some level, with the exception of 
vector-processing hardware such as the Cray computers.

What is your goal here?

If you want Python code without using a for or while statement consider:

 >>> def foo((a,b)):
...   if b%2:return a
...   else:return a+5
 >>> l = [1, 2, 3, 4]
 >>> map(foo,zip(l,range(len(l))))
[1, 7, 3, 9]

The test b%2 identifies elements of the list that have indexes not 
divisible by 2 and does not modify them. The other items are modified (in 
this case by adding 5. Change the test as needed to identify the items you 
want to modify.

Bob Gailer
bgailer@alum.rpi.edu
303 442 2625


--=====================_6362669==.ALT
Content-Type: text/html; x-avg-checked=avg-ok-17912FEA; charset=us-ascii
Content-Transfer-Encoding: 8bit

<html>
<body>
At 10:20 PM 6/11/2003 -0400, R. Alan Monroe wrote:<br><br>
<blockquote type=cite class=cite cite>I know I can modify every nth list
item using a for loop over a<br>
range(start,end,n), but can it be done in a single assignment,
without<br>
a loop?</blockquote><br>
No. Any operation on multiple elements of a list in any language is going
to be implemented as a loop at some level, with the exception of
vector-processing hardware such as the Cray computers.<br><br>
What is your goal here?<br><br>
If you want Python code without using a for or while statement
consider:<br><br>
<tt>&gt;&gt;&gt; def foo((a,b)):<br>
...&nbsp;&nbsp; if b%2:return a<br>
...&nbsp;&nbsp; else:return a+5<br>
&gt;&gt;&gt; l = [1, 2, 3, 4]<br>
&gt;&gt;&gt; map(foo,zip(l,range(len(l))))<br>
[1, 7, 3, 9]<br><br>
</tt>The test b%2 identifies elements of the list that have indexes not
divisible by 2 and does not modify them. The other items are modified (in
this case by adding 5. Change the test as needed to identify the items
you want to modify.<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625<br>
</body>
</html>


--=====================_6362669==.ALT--

--=======26455778=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-17912FEA
Content-Disposition: inline


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.488 / Virus Database: 287 - Release Date: 6/5/2003

--=======26455778=======--