How to delete a range of values from an array?
Use slice!
:
Deletes the element(s) given by [...] a range.
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array.slice!(2..5)
array #=> [1, 2, 7, 8, 9]
Remove a range of elements from an array
The second param of Array.prototype.splice() method is the number of elements to be removed and not an ending index.
You can see from the Array.prototype.splice() MDN Reference that:
Parameters
start
Index at which to start changing the array (with origin 0). If
greater than the length of the array, actual starting index will be
set to the length of the array. If negative, will begin that many
elements from the end of the array (with origin 1) and will be set to
0 if absolute value is greater than the length of the array.
deleteCount
Optional An integer indicating the number of old array
elements to remove. If deleteCount is 0, no elements are removed. In
this case, you should specify at least one new element. If deleteCount
is greater than the number of elements left in the array starting at
start, then all of the elements through the end of the array will be
deleted.
Solution:
You need to calculate the number of elements between these two indexes, so use b-a+1
to get the correct count.
Demo:
This is how should be your code:
var fruits = ["Banana", "Orange1", "Apple", "Banana", "Orange", "Banana", "Orange", "Mango", "Bananax", "Orangex"];var a = fruits.indexOf("Apple");var b = fruits.indexOf("Mango");
var removedFruits = fruits.splice(a, b-a+1);
console.log(fruits);
Python - How to delete some values in an array within a specific range?
Is it what you need? I have added a small bit to take care of the format of your entry data. I assume it is a text list with vectors. If not, you can change it accordingly.
I have a version with comprehension lists but it is quite ugly to read.
The output is 'list_float'.
I assumed you wanted to keep the first vector that are in the range of other ones, and remove the followings
# Make sure the format of your input is correct
list = ['83. 0.', '131. 0.', '178. 0.', '179. 0.', '227. 0.']
list_float = []
for point in list:
head, _, _ = point.partition(' ')
list_float.append(float(head))
# This is the bit removing the extra part
for pos, point in enumerate(list_float):
for elem in list_float[pos+1:]:
if (elem < point+5.) and (elem > point-5.):
list_float.remove(elem)
print(list_float)
Angular 9 How to delete a range of objects inside an array having indexes range specified by a user?
Your main problem is to get the remaining object after deleting. Here Array.slice function comes for the rescue.
Array.slice does not modify the original array. It just returns a new array of elements which is a subset of the original array.
Array.slice signature
arr.slice(startIndex, endIndex);
Consider the following array:
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8];
To get a slice of an array from values [2, 3, 4, 5], we write:
var slicedArr = arr.slice(2, 6);
Notice that, here, we gave the second argument as 6 and not 5.
After executing the above code, we get the values as:
arr // [0, 1, 2, 3, 4, 5, 6, 7, 8]
slicedArr // [2, 3, 4, 5]
How to delete a certain range from a numpy array?
There are probably more elegant solutions but this seems to work:
# get index where you observe the drop
ind_drop = np.where(np.diff(a) < 0)[0] + 1 # or np.argmin(np.diff(a)) + 1
# get index from start of the range which should be deleted
ind_low = np.argmin(a < a[ind_drop])
# delete the requested range
a_new = np.delete(a, np.arange(ind_low, ind_drop, 1))
That yields
array([ 887., 895., 903., 905., 914., 924., 934., 944.,
954., 965., 975., 986., 996., 1007.])
Some explanation:
One has to find the indices at which the array should be cut. The second index, ind_drop
, is there where we observe the drop i.e. there where the difference between two elements becomes negative for the first time.
np.diff(a)
array([ 8, 8, 8, 9, 8, 8, 8, 8, 9, 8, 8, 8, -80,
9, 10, 10, 10, 10, 11, 10, 11, 10, 11])
We can get this index by using the a Boolean array
np.diff(a) < 0
array([False, False, False, False, False, False, False, False, False,
False, False, False, True, False, False, False, False, False,
False, False, False, False, False], dtype=bool)
and applying np.where
.
np.where(np.diff(a) < 0)
(array([12]),)
Alternatively, you can also use:
np.argmin(np.diff(a)) + 1
The first index - where we start to cut - we get based on the value corresponding to ind_drop
a[ind_drop]
array([905])
So we need to find the index of the first element which is larger than this value which we can achieve by applying np.argmin
to a Boolean array:
a < a[ind_drop]
array([ True, True, True, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False], dtype=bool)
np.argmin
returns the (first) index of the minimal value in an array; it works on the Boolean array as True
is 1, and False
is 0:
np.argmin(a < a[ind_drop])
3
Now that we have both indexes, we can use np.delete
to remove all elements between these indexes:
np.arange(ind_low, ind_drop, 1)
array([ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
yielding the desired output.
Swift remove objects in Array range
Use removeSubrange
method of array. Make a valid range
by element location
and length
.
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let range = 1...3
array.removeSubrange(range)
print(array)
Output: [1, 5, 6, 7, 8, 9, 10]
Note: Range should be a valid range I mean it should not be out from array.
Here is yours way (by for loop)
We can not remove objects by their indexes in a loop because every time object removes array's count and objects indexes will be change so out of range crash can come or you might get a wrong output. So you will have to take help of another array. See below example:-
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var newArray: [Int] = []
let minRange = 1
let maxRange = 3
for i in 0..<array.count {
if i >= minRange && i <= maxRange {
/// Avoid
continue
}
newArray.append(array[i])
}
print(newArray)
Output: [1, 5, 6, 7, 8, 9, 10]
How to delete multiple items of an array by value?
Loop in reverse order or build a new array with the items that are not to be removed.
Related Topics
Generate Random String Based on Regex
How to Use Link_To to Link an Image and a Text
How to Source Environment Variables for a Command Shell in a Ruby Script
Ruby on Rails:Rails Generate Controller Does Not Work
Sorting a Hash in Ruby by Its Value First Then Its Key
Rails Active Record: Find in Conjunction with :Order and :Group
Rails Routes: Get Without Param :Id
Rails:Runtimeerror - Can't Modify Frozen Array When Running Rspec in Rails
What's the Best Way to Implement Acls to a Rails Application
Unicode Characters in a Ruby Script
Gem Which Cannot Find Gem Despite It Being Installed
How to Sign_In for Devise to Test Controller with Minitest
What's the Difference Between /\P{Alpha}/I and /\P{L}/I in Ruby
Finding the Product of a Variable Number of Ruby Arrays
How to Tell Which Openssl Lib Is Actually Being Used by an Rvm-Installed Ruby