This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
How do I pass a variable by reference?
(39 answers)
Closed 4 years ago.
for some reason the variable "a" in the main function changes after the function call. In spite of the fact that "a" doesn't even change within the function. Although even if it did, such changes should not affect its value outside the function, right?
I am using Python 3.6, and I'm out of ideas as to why this is happening. Any help would be greatly appreciated
def mhmArgSort(myNumListInput):
myNumList=myNumListInput
cnt=0
endCnt=len(myNumList)
NumListSortedIndices=[]
for cnt1 in range(len(myNumList)):
smallestNum=100000000000
smallestNumIndex=0
for cnt in range(len(myNumList)):
if myNumList[cnt]<smallestNum:
smallestNum=myNumList[cnt]
smallestNumIndex=cnt
NumListSortedIndices.append(smallestNumIndex)
myNumList[smallestNumIndex]=100000000000
return NumListSortedIndices
a=[1,2,3,4,-1,0,3]
print(a)
b=mhmArgSort(a)
print(a)
print(b)
This produces the following result:
[1, 2, 3, 4, -1, 0, 3]
[100000000000, 100000000000, 100000000000, 100000000000, 100000000000, 100000000000, 100000000000]
[4, 5, 0, 1, 2, 6, 3]
Related
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 3 years ago.
In python 3.7.4 I wnat to sort the array but I can't. I used the function sort and wrote this code -
arr = [0,5,8,9,6,3,1,2]
print(arr.sort())
output
None
Please help me.
Python's list.sort() modifies the list in-place and does not return it. For example:
x = [0,5,8,9,6,3,1,2]
print(x) # [0, 5, 8, 9, 6, 3, 1, 2]
x.sort()
print(x) # [0, 1, 2, 3, 5, 6, 8, 9]
See the docs for more information on list methods.
This question already has answers here:
Modifying a list inside a function
(4 answers)
How do I pass a variable by reference?
(39 answers)
Understanding Python's call-by-object style of passing function arguments [duplicate]
(3 answers)
Closed 4 years ago.
I am new to python. I searched a lot but couldn't find the reason why this is happening. Could anyone please tell me the difference between these two?
My general question is when using a list in function is by reference and when is by value?
My test function is acting as call by reference and test2 is acting as call by value.
I know in python everything is an object, but with that, I couldn't understand the difference.
tnx
def test(my_list):
for i in range(len(my_list)):
my_list[i] = 5
def test2(my_list1):
my_list1 = [6, 6, 6]
a = [4, 4, 4]
print(a)
test(a)
print(a)
test2(a)
print(a)
output:
[4, 4, 4]
[5, 5, 5]
[5, 5, 5]
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 6 years ago.
Why does the code below output {'a': [1, 3, 4, 5, 6, 7, 8, 9], 'b': [1, 3, 4, 5, 6, 7, 8, 9]}
adic = {}
main_array = [1,2,3,4,5,6,7,8,9]
adic["a"] = main_array
adic["b"] = main_array
array = adic["a"]
array.remove(2)
print(adic)
I am not even assigning the new array to a key. Why should 2 be automatically removed from all the arrays in the dictionary. Am i missing something vital?
All your references point to the same list, as you can check by adding:
print(id(adic["a"]))
print(id(adic["b"]))
print(id(array))
print(id(main_array))
On my system:
32707888
32707888
32707888
32707888
Assignment does not create a copy.
There are several ways to copy a list if this is what you want, such as
new_list = list(old_list)
discussed here.
The reason why your output is doing that is because of the lines where you set each adic['value'] = main_array each of those statements is pointing to the same memory address of your main_array so when you do array = adic["a"] that's also pointing to the same memory address and when you remove 2 it's removing 2 from main_array. Because you are removing from main_array, everything that pointed to main_array is also affected.
This question already has answers here:
"Least Astonishment" and the Mutable Default Argument
(33 answers)
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 7 years ago.
I am confused trying to solve a task of "flattening" a list of lists.
For example, I've got a list [1, [2, 2, 2], 4] - I need to transform it to [1, 2, 2, 2, 4].I solved it that way:
def flat_list(array, temp_list=[]):
for item in array:
if str(item).isdigit():
temp_list.append(item)
else:
temp_list = flat_list(item, temp_list)
return temp_list
But when I test it:
assert flat_list([1, 2, 3]) == [1, 2, 3]
assert flat_list([1, [2, 2, 2], 4]) == [1, 2, 2, 2, 4]
assert flat_list([[[2]], [4, [5, 6, [6], 6, 6, 6], 7]]) == [2, 4, 5, 6, 6, 6, 6, 6, 7]
the value of "temp_list" is being transferred between function calls. The value of "temp_list" in the beginning of the second assert is [1, 2, 3] - the value of "return" of first assert - but not the "[]".
As I guess, this is because I have some misunderstanding of scopes and "return" instruction but I don't understand what exactly.
It's a known issue in Python.
Default parameter values are always evaluated when, and only when, the
“def” statement they belong to is executed
Ref: http://effbot.org/zone/default-values.htm
In your code example, the temp_list's default value is evaluated when the def statement is executed. And thus it's set to an empty list. On subsequent calls, the list just keeps growing because the temp_list's default values are no longer evaluated and it keeps using the list instance it created the first time.
This could be a work around:
def flat_list(array, temp_list=None):
if not temp_list:
temp_list = []
for item in array:
if str(item).isdigit():
temp_list.append(item)
else:
temp_list = flat_list(item, temp_list)
return temp_list
Here, we have passed None instead of the empty list. And then inside the function body we check the value and initialize an empty list as we need.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
So i'm looking for sort of a layman/beginner explanation as to what this means:
L[i:i+lensub]
What does that mean by it's self? by the the way lensub = len(sublist) and L is list.
What that code is doing is using Explain Python's slice notation to slice L from position i to i+lensub.
The format for slice notation is [start:stop:step].
Below is basic demonstration:
>>> lst = [1, 2, 3, 4, 5]
>>> # Get positions 1 to 3
>>> lst[1:3]
[2, 3]
>>>