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
Related
This question already has answers here:
How to stop the input function from inserting a new line?
(9 answers)
How to convert N space separated numbers into in array in Python? [closed]
(2 answers)
Closed 11 months ago.
I want the input to be like
2 3 4 5
and not like
2
3
4
5
Is there any way to do it by adding end='' or some other way in the input() function?
This is part of a recursive function called makearray(). I want it to take spaced inputs and not on new lines.
l = int(float(input("Enter number in array 1:"end='')))
if (l > 0):
global p
p = np.append(p, l * l)
else:
pass
makearray(b - 1)
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
This question already has answers here:
How do you create different variable names while in a loop? [duplicate]
(9 answers)
Closed 2 years ago.
I want to declare variable name in sequence:
Something like x_1, x_2, x_3,......., x_5 and assign value with them in python.
Something like x_i = i^2.
n = 10
for i in range(1, n):
x_i = i**2
print(x_2)
Can someone please help?
You could use this but its not recommended:
n = 10
for i in range(1, n):
globals()[f'x_{i}'] = i**2
print(x_2) #4
This question already has answers here:
Check between which floats in a list a given float falls
(2 answers)
Closed 4 years ago.
getindex(x,y)
Input: a value x, and a sorted array y[:] (no repeat element)
Output: the index i, such that y[i-1] <= x <y[i]
The time complexity should be O(log(N))
Is there a Python/Numpy function we can use?
For example:
y[0]=-0.2
y[1]=1.5
y[2]=1.9
y[3]=3.2
Then
getindex(-4.0,y) returns 0
getindex(0.5,y) returns 1
getindex(6.0,y) returns 4
numpy.searchsorted
I find it here:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.searchsorted.html
This question already has answers here:
Is there a zip-like function that pads to longest length?
(8 answers)
Zipping lists of unequal size
(5 answers)
Closed 6 years ago.
I want to print different size lists side by side.
I'm using
In: for n,g in zip(ten_pos,real_pos):
print (n + "\t\t\t\t\t\t" + g)
But If one list has 5 item and the other one has 20, It only prints 5 and 5 and I want 5 and 20.
Is there an easy way to solve this?
Check out itertools.zip_longest.
import itertools
for a, b in itertools.zip_longest([1,2], [3,4,5]):
print(a, b)
# outputs
1 3
2 4
None 5
You can change the value used to fill with the kwarg fillvalue.