Why will one loop modify a list of lists, but the other wont [duplicate]

2024/10/15 11:21:25

One of the answers in "python way" to parse and conditionally replace every element in 2D list has revealed to me that I understand even less than I thought.

Given a list of lists containing strings:

myLists = [['5','cat','23'], ['33','parakeet','scalpel'], ['correct','horse','battery','staple','99']]  

I wish to replace each element with the return of:

def numParser(string):try:return int(string)except ValueError:return string

My original code was this, but it does not modify the original array. I assumed because list is a copy, not the actual list (am I wrong?):

for single_list in myLists:single_list = map(numParser, rawData)

And one of the answers which modifies the lists in place is:

for single_list in myLists:for i, item in enumerate(single_list):single_list[i] = numParser(item)

Why does the second solution work but not the first?

Answer

This statement:

single_list = ...

assigns a value to the local variable named single_list. It does not have any effect on any other data.

The statement:

single_list[i] = ...

modifies the value of the object referenced by the local variable named single_list, specifically it sets the value of a particular element of the list.

https://en.xdnf.cn/q/117839.html

Related Q&A

Python CGI executable script downloads / shows script code

A script I wrote long ago (getWords.py) used to be executable at my localhost (http://local.example.com/getWords.py?query-string)My python script starts like this :#!/usr/bin/env python # chmod 755 ge…

My function returns None

I am new to Python and I was trying to solve this exercise, but keep getting None output. The question asked for a program in which the input is hours and rate and the output is the gross pay, includin…

Django undefined symbol: PyUnicode_AsUTF8

I am new to Python/Django. I have set up the environment needed to run Django project.When Im trying to migrate an existing project , it shows up this errordjango.core.exceptions.ImproperlyConfigured: …

using a variable keyword for an optional argument name with python argparse

I am using argparse for a python script I am writing. The purpose of the script is to process a large ascii file storing tabular data. The script just provides a convenient front-end for a class I have…

Error while setting up MongoDB with django using django mongodb engine on windows

Steps I followed :pip install git+htp://github.com/django-nonrel/[email protected]It did not work, so I downloaded the zip from the site "htp://github.com/django-nonrel/django" and pasted the…

Python login page with pop up windows

I want to access webpages and print the source codes with python, most of them require login at first place. I have similar problem before and I have solved it with the following code, because they are…

Calculate Scipy LOGNORM.CDF() and get the same answer as MS Excel LOGNORM.DIST

I am reproducing a chart in a paper using the LOGNORM.DIST in Microsoft Excel 2013 and would like to get the same chart in Python. I am getting the correct answer in excel, but not in python.In excel …

Python MySQLdb cursor.execute() insert with varying number of values

Similar questions have been asked, but all of them - for example This One deals only with specified number of values.for example, I tried to do this the following way:def insert_values(table, columns, …

Searching in a .txt file and Comparing the two values of a string in python?

"cadence_regulatable_result": "completeRecognition","appserver_results": {"status": "success","final_response": 0,"payload": {"…

How to perform an HTTP/XML authentication with requests

I am trying to authenticate to Docushare with Python 3.4 using requests 2.7. I am relatively new to Python and to the requests module but Ive done a lot of reading and am not able to make any more prog…