Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Modifying Element In For List

Reply
Thread Tools

Modifying Element In For List

 
 
octopusgrabbus
Guest
Posts: n/a
 
      11-15-2010
My question concerns elementary list and pass by reference:

I've written a function which is passed a list that contains rows read
from a csv file. The function traverses csv_rows, row by row, and
inspects the first element in each row. The function tests for '', and
if true, replaces that with a 0.

I've used the standard Python for syntax for this.

def cleanMeterID(csv_rows, bad_meter_id_count):
d = drIdx()

row_number = 0

for row in csv_rows:
if False == is_number(row[d.MeterID]):
csv_rows[row_number][d.MeterID] = 0
row_number = row_number + 1
bad_meter_id_count[0]= bad_meter_id_count[0] + 1

print("Received ", bad_meter_id_count[0], " bad meter ids")

I believe the logic show above is flawed, because I am not modifying
elements in the original csv_rows list. I would modify this to use an
index to traverse the list like

idx=None
for idx in range(0, len(csv_rows), 1):
if False == is_number(row[d.MeterID]):
csv_rows[row_number][d.MeterID] = 0
row_number = row_number + 1
bad_meter_id_count[0]= bad_meter_id_count[0] + 1

Is this correct?

Thank you.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-16-2010
On Mon, 15 Nov 2010 08:31:52 -0800, octopusgrabbus wrote:

> My question concerns elementary list and pass by reference:


What does pass-by-reference have to do with Python? Python doesn't use
pass-by-reference... if you think it does, you have misunderstood
something.

Hint: if you think Python has pass-by-reference, please write a procedure
that takes two generic arguments, and swaps their values, like so:

a = 1
b = 2
swap(a, b)
assert a == 2
assert b == 1

This is the canonical test for pass by reference semantics.


> I've written a function which is passed a list that contains rows read
> from a csv file.



What are the rows? Lists? Tuples? Something else?


> The function traverses csv_rows, row by row, and
> inspects the first element in each row. The function tests for '', and
> if true, replaces that with a 0.
>
> I've used the standard Python for syntax for this.


As opposed to C++ syntax or Lisp syntax?

You can't write Python code without using Python syntax, so I'm not sure
what you mean here.


> def cleanMeterID(csv_rows, bad_meter_id_count):
> d = drIdx()
>
> row_number = 0
>
> for row in csv_rows:
> if False == is_number(row[d.MeterID]):
> csv_rows[row_number][d.MeterID] = 0
> row_number = row_number + 1
> bad_meter_id_count[0]= bad_meter_id_count[0] + 1
>
> print("Received ", bad_meter_id_count[0], " bad meter ids")
>
> I believe the logic show above is flawed, because I am not modifying
> elements in the original csv_rows list.



Are you sure about that? What happens when you try it?



> I would modify this to use an
> index to traverse the list like


This is nearly always the wrong solution in Python.




--
Steven
 
Reply With Quote
 
 
 
 
Johannes Bauer
Guest
Posts: n/a
 
      11-19-2010
Am 15.11.2010 18:27, schrieb Duncan Booth:

> Comparing directly against True or False is error prone: a value in
> Python can be false without actually being equal to False.


Well, you can always use "is" instead of "==", which makes a comparison
to True or False perfectly safe.

Regards,
Johannes

--
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?

> Zumindest nicht öffentlich!

Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$(E-Mail Removed)>
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      11-19-2010
On 11/19/2010 12:17 PM, Johannes Bauer wrote:
> Am 15.11.2010 18:27, schrieb Duncan Booth:
>
>> Comparing directly against True or False is error prone: a value in
>> Python can be false without actually being equal to False.

>
> Well, you can always use "is" instead of "==", which makes a comparison
> to True or False perfectly safe.
>

But it's still the wrong thing to do.

if condition==True:

and

if condition is True:

should both be replaced (under most circumstances) by

if condition:

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon 2011 Atlanta March 9-17 http://us.pycon.org/
See Python Video! http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
modifying a list element from a function TP Python 6 04-02-2009 09:07 AM
HELP --- Modifying a instance of a specific element nad407 Java 0 01-18-2009 03:44 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Modifying every alternate element of a sequence jm.suresh@no.spam.gmail.com Python 9 11-28-2006 08:08 PM
Modifying the value of each element stored in a std::vector. Jason Heyes C++ 11 02-09-2005 08:23 PM



Advertisments