Why is the index value changing? [duplicate] - python

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 1 year ago.
Here is my code.
import numpy as np
m = 2
n = 2
arr = [[0]*m]*n
for i in np.arange(n):
for j in np.arange(m):
print(i)
arr[i][j] = i
print(arr)
the output is:
0
0
1
1
[[1,1],[1,1]]
I do not understand why the output array is not [[0,0],[1,1]].
Please I am losing my mind.

Because [0]*m object is being repeated n times. Whatever your i you're changing the same object, so you see only the last change printed.
arr = [[0]*m for _ in range(n)] solves the problem

Related

Python find out duplicates in array using function [duplicate]

This question already has answers here:
How do I find the duplicates in a list and create another list with them?
(42 answers)
Closed 2 years ago.
Example 1:
input: [1,3,2,1,3,5]
Output: [1,3]
def duplicateNumber(mylist):
??
Thanks
Next time at least make an honest effort, describe the problem you are facing and then ask a question :
def duplicateNumber(x):
_size = len(x)
repeated = []
for i in range(_size):
k = i + 1
for j in range(k, _size):
if x[i] == x[j] and x[i] not in repeated:
repeated.append(x[i])
return repeated

How can I subtract a list from a list for its value inside the list? [duplicate]

This question already has answers here:
Subtracting 2 lists in Python
(16 answers)
Closed 2 years ago.
So I want to be able to subtract a list to another list.
For example, if I had two lists:
x = [0,8,10]
y = [1,7,9]
I would like to be able to basically subtract y[i] - x[i] using a FOR LOOP.
So ultimately, inside the loop, it would go 1-0, 7-8, 9-10, and so on if the list is longer.
Additionally, would there be any way to check if all or only one of the differences are equal to each other?
Thank you.
Try this :
import operator
x = [0,8,10]
y = [1,7,9]
ans = list(map(operator.sub, y, x))
print(ans)
Output :
[1, -1, -1]

What is the difference between a=a*7 and a *= 7 [duplicate]

This question already has answers here:
Why does += behave unexpectedly on lists?
(9 answers)
Why does python/numpy's += mutate the original array?
(4 answers)
Closed 2 years ago.
I have written the below codes to make 2 functions.
The only difference is the second line.(input_array *= 7 and input_array = input_array * 7)
But they gave completely different results. (The results are in the picture attached)
I wonder why is that and what's the diefference between the two second lines?
def array_times_seven(input_array):
input_array *= 7
return input_array
test_array = np.ones((5,5))
array_times_seven(test_array[3:,3:])
test_array
#-----------------------------------------------
def array_times_seven(input_array):
input_array = input_array * 7
return input_array
test_array = np.ones((5,5))
array_times_seven(test_array[3:,3:])
test_array
Result for the 1st code
result for the 2nd code

list of list i want to change exactly one component [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 3 years ago.
in 19x19 matrix with all components == 0 now (list of list a)
for example, i want to change a[1][1] to 1
but they changed all index(1) of lists of list(a)
emphasized text tried to stop using for- sentence and changed to while-, just typed several times not using loops.
a = []
row = []
for i in range(19) :
row.append(0)
for i in range(19) :
a.append(row)
#19x19 matrix a has been made
n = int(input())
for j in range(n) :
x, y = map(int, input().split())
a[x-1][y-1] = 1
for k in a :
print(k)
You are adding the same row again and again.
a = []
row = []
for i in range(19) :
row.append(0)
for i in range(19) :
a.append(row[:]) #slice it to make a copy

Get a fixed number of items from a generator [duplicate]

This question already has answers here:
How to get the n next values of a generator into a list
(5 answers)
Closed 5 years ago.
What is the most efficient way to get a fixed number of items from a generator?
What I currently do is using zip and range. In this example I take
chunks of size 3 from the generator.
def f():
x = 0
while x < 21:
yield x
x += 1
g = f()
while True:
x = [i for _, i in zip(range(3), g)]
if not x:
break
print x
The background is that the database I use provides a generator object for query results. Than I fill a fixed size numpy array with data and process it as one batch.
Use itertools.islice:
import itertools
for elem in itertools.islice(f(), 3):
print elem
and directly into your numpy array:
my_arr = np.array(itertools.islice(f(), 3))

Categories

Resources