An alternative solution is provided by np.intersect1d
:
import numpy as np
array1 = [1, 2, 3, 4, 5, 6]
array2 = [2, 6, 3, 4, 1, 5]
np.intersect1d(array1, array2, return_indices = True)[2]
A list comprehension helps:
positions = [array2.index(item) for item in array1]
A for loop with the same result:
positions = [] for item in array1: positions.append(array2.index(item))
If you don’t have repeating elements in array2
you can use the following solution. It should be faster than the list comprehension with index()
:
from operator
import itemgetter
from itertools
import count
array1 = [1, 2, 3, 4, 5, 6]
array2 = [2, 6, 3, 4, 1, 5, 7, 8]
itemgetter( * array1)(dict(zip(array2, count())))
#[4, 0, 2, 3, 5, 1]
In this example, you will learn to write a JavaScript program that will check if a number is a float or an integer value.,In the above example, the regex pattern is used to check if the passed argument is an integer value or float value.,In the above program, the passed value is checked if it is an integer value or a float value.,To understand this example, you should have the knowledge of the following JavaScript programming topics:
Example 1: Using Number.isInteger()
// program to check if a number is a float or integer value
function checkNumber(x) {
// check if the passed value is a number
if (typeof x == 'number' && !isNaN(x)) {
// check if it is integer
if (Number.isInteger(x)) {
console.log(`${x} is integer.`);
} else {
console.log(`${x} is a float value.`);
}
} else {
console.log(`${x} is not a number`);
}
}
checkNumber('hello');
checkNumber(44);
checkNumber(3.4);
checkNumber(-3.4);
checkNumber(NaN);
Output
hello is not a number
44 is integer.
3.4 is a float value. -
3.4 is a float value.
NaN is not a number
Example 2: Using Regex
// program to check if a number is a float or integer value
function checkNumber(x) {
let regexPattern = /^-?[0-9]+$/;
// check if the passed number is integer or float
let result = regexPattern.test(x);
if (result) {
console.log(`${x} is an integer.`);
} else {
console.log(`${x} is a float value.`)
}
}
checkNumber(44);
checkNumber(-44);
checkNumber(3.4);
checkNumber(-3.4);
I have one array of value and I want to find anycodings_arrays what position it has in another array of anycodings_arrays values. So for example if I have:,I want to find what position each element anycodings_arrays from array 1 has in array 2, so I want it to anycodings_arrays return something like,If you don’t have repeating anycodings_arrays elements in array2 you can use the anycodings_arrays following solution. It should be faster anycodings_arrays than the list comprehension with anycodings_arrays index():,Which i guess means I can't use .index on a anycodings_arrays float. Is there another way I can go about anycodings_arrays this.
I have one array of value and I want to find anycodings_arrays what position it has in another array of anycodings_arrays values. So for example if I have:
array1 = [1, 2, 3, 4, 5, 6] array2 = [2, 6, 3, 4, 1, 5, .....]
I want to find what position each element anycodings_arrays from array 1 has in array 2, so I want it to anycodings_arrays return something like
what_position = [4, 0, 2, 3, 5, 1]
I've tried something like this:
for i in range(len(array1)):
what_position = array1[i].index(array[2])
An alternative solution is provided by anycodings_arrays np.intersect1d:
import numpy as np
array1 = [1, 2, 3, 4, 5, 6]
array2 = [2, 6, 3, 4, 1, 5]
np.intersect1d(array1, array2, return_indices = True)[2]
A list comprehension helps:
positions = [array2.index(item) for item in array1]
A for loop with the same result:
positions = [] for item in array1: positions.append(array2.index(item))
If you don’t have repeating anycodings_arrays elements in array2 you can use the anycodings_arrays following solution. It should be faster anycodings_arrays than the list comprehension with anycodings_arrays index():
from operator
import itemgetter
from itertools
import count
array1 = [1, 2, 3, 4, 5, 6]
array2 = [2, 6, 3, 4, 1, 5, 7, 8]
itemgetter( * array1)(dict(zip(array2, count())))
#[4, 0, 2, 3, 5, 1]
Last Updated : 13 Jan, 2022
1000 / (100 / 10 / 2)