Replacing specific value in a 2D array
When the mainArr
array is created using the fill
method, new Array(8).fill([0, 0, 0, 0, 0, 0, 0, 0])
, in the new created array all the elements ([0, 0, 0, 0, 0, 0, 0, 0]
) are refrenced to the same value in the memory.
So when you try to update one value in an array because all arrays are linked hence the value is changed in all of them.
To make an array that is not linked like this you can make it sort of manually using a loop, like this:
const mainArr = [];
for (let i = 0; i < 8; i++) {
const row = [];
for (let i = 0; i < 8; i++) row.push(0);
mainArr.push(row);
}
For updating, you can do it much more easily like this:
mainArr[whiteQueen[0]][whiteQueen[1]] = 1;
mainArr[blackQueen[0]][blackQueen[1]] = 1;
The above code will replace the exact value with 1
.
Final Code:
function generatedBoard(whiteQueen, blackQueen) {
const mainArr = [];
for (let i = 0; i < 8; i++) {
const row = [];
for (let i = 0; i < 8; i++) row.push(0);
mainArr.push(row);
}
mainArr[whiteQueen[0]][whiteQueen[1]] = 1;
mainArr[blackQueen[0]][blackQueen[1]] = 1;
return mainArr;
}
let whiteQueen = [0, 5];
let blackQueen = [5, 0];
console.log(generatedBoard(whiteQueen, blackQueen));
replace values with condition in a multidimensional array python
NumPy module usually is of more use in the case of execution time. Let's have an example in a jupyter notebook:
import numpy as np
matrix = np.random.rand(400,400,400)
Next cell:
%%time # This is a magic command that can be used in a colab notebook in order to see the execution time. If you run it through Python command line, it probably results in a error.
matrix[matrix < 0.5] = np.nan
Output
CPU times: user 625 ms, sys: 0 ns, total: 625 ms
Wall time: 773 ms
As you see, it just took 773 ms
to reevaluate the elements of a matrix of 400*400*400
elements. And of course, it works! Let's have a look at matrix
output now:
array([[[0.58888172, 0.7849002 , nan, ..., nan,
nan, nan],
[ nan, 0.76474748, nan, ..., nan,
nan, nan],
[0.60103106, 0.62287764, nan, ..., 0.63933367,
nan, nan],
...,
[ nan, 0.91627877, 0.52837604, ..., nan,
nan, 0.7755497 ],
[0.52519088, nan, 0.70463779, ..., 0.80754958,
0.96804943, 0.66126769],
[0.90220552, 0.97406624, 0.93333412, ..., nan,
nan, nan]],
[[ nan, nan, 0.80116461, ..., 0.60552189,
nan, 0.60547716],
[ nan, nan, nan, ..., nan,
0.58920489, 0.64414314],
[0.59112547, 0.58970573, 0.7817127 , ..., 0.92115189,
0.67403891, 0.50897163],
...,
[ nan, 0.73571716, 0.89320417, ..., nan,
0.64638843, 0.90098355],
[ nan, 0.60734221, 0.54421301, ..., 0.67758928,
0.91324091, 0.56821318],
[ nan, nan, 0.72628094, ..., 0.79698288,
nan, nan]],
[[0.60481884, 0.87283859, 0.98231053, ..., nan,
0.90631025, 0.57994473],
[ nan, nan, nan, ..., nan,
0.92896717, nan],
[ nan, 0.85387808, nan, ..., nan,
0.82199092, 0.64892107],
...,
[0.51940221, nan, 0.86065574, ..., nan,
0.70869112, 0.60288317],
[0.92355508, nan, nan, ..., 0.61121525,
0.60234445, 0.88535996],
[0.94738293, nan, 0.7134501 , ..., 0.56876322,
nan, nan]],
...,
[[ nan, nan, 0.54247768, ..., nan,
nan, 0.94484817],
[ nan, nan, 0.6923426 , ..., nan,
nan, 0.72161344],
[0.64403315, nan, nan, ..., nan,
0.87946106, 0.92479187],
...,
[ nan, 0.62671375, 0.59782275, ..., nan,
nan, nan],
[ nan, 0.98113013, nan, ..., nan,
nan, nan],
[0.64293314, nan, 0.86640945, ..., 0.57533483,
nan, 0.74854156]],
[[0.97657601, nan, 0.92713931, ..., 0.63876501,
0.50303145, 0.55530041],
[0.85351375, nan, 0.65790311, ..., 0.9921048 ,
0.67602624, 0.99773446],
[ nan, 0.84887916, 0.7843213 , ..., nan,
0.91993127, nan],
...,
[0.53349779, 0.80875952, nan, ..., nan,
0.69806386, 0.94379129],
[0.91561332, nan, nan, ..., nan,
0.86274521, nan],
[ nan, nan, nan, ..., 0.71484682,
0.65998919, nan]],
[[ nan, 0.84276618, nan, ..., nan,
nan, 0.59601719],
[0.78492731, nan, nan, ..., nan,
nan, 0.86452704],
[ nan, 0.99587101, nan, ..., 0.7774153 ,
0.51868572, 0.54691035],
...,
[0.77281982, nan, 0.87166967, ..., 0.83113015,
0.85596892, nan],
[ nan, nan, 0.72000176, ..., nan,
nan, 0.74863166],
[0.52007484, nan, 0.58487587, ..., 0.55538366,
0.68744613, 0.52256696]]])
Long store short, you just need to change the format of your data
from a list (if it is) to a numpy
array: data = np.array(data)
.
Search and replace string in 2D Array in Swift
You don't have a 2D array, you have an Array of Dictionaries.
You can set all of the values for the owned
keys by iterating the indices of the Array and updating the values:
shopArray.indices.forEach { shopArray[$0]["owned"] = false }
That is the functional way to do it. You could also do the same operation with a for
loop:
for idx in shopArray.indices {
shopArray[idx]["owned"] = false
}
How to replace values in multidimensional array?
string.replace won't work, since it does not affect the original value.
>>> test = "hallo"
>>> test.replace("a", " ")
'h llo'
>>> test
'hallo'
Instead you need to access the list via the indexes:
for row in ArrayMulti:
for index in range(len(row)):
row[index] = "a"
If you provide a more precise question and add the output you want to achieve to the question, I can give you a more precise answer.
I scrapped the previous solution since it wasn't what you wanted
def UserMultiArray(usrstrng, Itmval):
ArrayMulti=[[" " for x in range(Itmval)] for x in range(Itmval)]
for index, char in enumerate(usrstrng):
ArrayMulti[index//Itmval][index%Itmval] = char
return ArrayMulti
>>> stack.UserMultiArray("funs", 3)
[['f', 'u', 'n'], ['s', ' ', ' '], [' ', ' ', ' ']]
This little trick uses the whole-number-division:
[0, 1 ,2 ,3 ,4] // 3 -> 0, 0, 0, 1, 1
and the modulo operator(https://en.wikipedia.org/wiki/Modulo_operation):
[0, 1 ,2 ,3 ,4] % 3 -> 0, 1, 2, 0, 1
Python 2D array replace value by NaN when other array is NaN
You can use np.isnan(A)
directly:
B = np.zeros_like(A)
B[np.isnan(A)] = np.nan
np.where
is useful for when you want to construct B
directly:
B = np.where(np.isnan(A), np.nan, 0)
Python: replace values in a 2d array from another 1d array based on some conditions
According to your post, you already almost got the answer. If you are really looking for a one line code, you can do this.
c = B[A]
c
Out[24]:
array([[0.2, 0.2],
[0.8, 0.9]])
The code above is for numpy array. On the other hand, if it is a list,
list comprehension would be required.
Related Topics
Python: How to Check If Cell in CSV File Is Empty
How to Remove Any Url Within a String in Python
Remove Partial String from Dataframe With Pandas
Converting a List into Comma Separated and Add Quotes in Python
Adding Different Sized/Shaped Displaced Numpy Matrices
How to Pass Variables from Python Script to Bash Script
How to Save\Load Models in Spark\Pyspark
Python - Get Path of Root Project Structure
How to Execute Two Commands in Terminal Using Python'S Subprocess Module
I Received an Error Message That I Don't Quite Understand
Deal With Overflow in Exp Using Numpy
How to Restart a Program Based on User Input
How to Select the Last Column of Dataframe
Can Anyone Explain Me What This Python 3 Command Do