Modifying Element In For List

O

octopusgrabbus

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.
 
S

Steven D'Aprano

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.
 
J

Johannes Bauer

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

--
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 <[email protected]>
 
S

Steve Holden

Am 15.11.2010 18:27, schrieb Duncan Booth:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top