Modifying Element In For List

Discussion in 'Python' started by octopusgrabbus, Nov 15, 2010.

  1. 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.
    octopusgrabbus, Nov 15, 2010
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Nov 16, 2010
    #2
    1. Advertising

  3. 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$>
    Johannes Bauer, Nov 19, 2010
    #3
  4. octopusgrabbus

    Steve Holden Guest

    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/
    Steve Holden, Nov 19, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Heyes
    Replies:
    11
    Views:
    743
    Jason Heyes
    Feb 9, 2005
  2. Replies:
    9
    Views:
    286
  3. HANM
    Replies:
    2
    Views:
    716
    Joseph Kesselman
    Jan 29, 2008
  4. nad407
    Replies:
    0
    Views:
    285
    nad407
    Jan 18, 2009
  5. TP
    Replies:
    6
    Views:
    233
    Gabriel Genellina
    Apr 2, 2009
Loading...

Share This Page