so I have an array and I want to add 1 to all the items.
var arr = [2, 3, 6, 9]
for (index, x) in enumerate(arr) {
arr[index] = arr[index] + 1
}
is there a simpler version of this? there's no reason to have 'x' in there. I know there's the alternate way of writing it this way:
arr[index] = x + 1
but that doesn't seem like enough reason to have 'x' there.
You can iterate indices of the array
var arr = [2, 3, 6, 9]
for index in indices(arr) {
arr[index] += 1
}
Essentially, indices(arr) is the same as arr.startIndex ..< arr.endIndex, but it's simple :)
OR, in this specific case, you might want to:
arr = arr.map { $0 + 1 }
Yes, this is a really good use case for the .map function.
var arr = [2, 3, 4]
arr = arr.map({$0 + 1})
// arr would now be [3, 4, 5]
Related
I have two sorted arrays
array1 = [0, 3, 4, 31]
array2 = [4, 6, 30]
I try to sort these arrays by using the code below:
def mergeSortedArray(array1, array2):
if not len(array1):
return array2
if not len(array2):
return array1
mergedArray = []
array1Item = array1[0]
array2Item = array2[0]
i = 0
j = 0
while (i < len(array1)) and (j < len(array2)):
if array1Item < array2Item:
mergedArray.append(array1Item)
array1Item = array1[i + 1]
i += 1
else:
mergedArray.append(array2Item)
print(j)
array2Item = array2[j + 1]
j += 1
return mergedArray
print(mergeSortedArray([0, 3, 4, 31], [4, 6, 30]))
But the terminal keep telling me that:
line 26, in mergeSortedArray
array2Item = array2[j + 1]
IndexError: list index out of range
I wonder which part I did wrong! Can someone explain to me? plz~
BTW, what is the syntactically cleanest way to accomplish this?
Make use of Python's features, such as merging two lists with the + operator. Then, simply sort the new list.
>>> array1 = [0, 3, 4, 31]
>>> array2 = [4, 6, 30]
>>> merged_array = array1 + array2
>>> merged_array.sort()
>>> merged_array
[0, 3, 4, 4, 6, 30, 31]
If I have an array like this, how can I remove just one of the 4:rs if I can't use .remove(at:):
let a = [3, 4, 4, 5, 4, 8, 7]
You need to get the index of the first element where value = 4 and then remove it. Here is the code:
import Foundation
var a = [3, 4, 4, 5, 4, 8, 7]
let b = a.firstIndex(of: 4)
if let b = b {
a.remove(at: b)
} else {
print("value not present in array")
}
print(a)
Is there a concise way in Swift of creating an array by applying a binary operation on the elements of two other arrays?
For example:
let a = [1, 2, 3]
let b = [4, 5, 6]
let c = (0..<3).map{a[$0]+b[$0]} // c = [5, 7, 9]
If you use zip to combine the elements, you can refer to + with just +:
let a = [1, 2, 3]
let b = [4, 5, 6]
let c = zip(a, b).map(+) // [5, 7, 9]
Update:
You can use indices like this:
for index in a.indices{
sum.append(a[index] + b[index])
}
print(sum)// [5, 7, 9]
(Thanks to Alexander's comment this is better, because we don't have to deal with the element itself and we just deal with the index)
Old answer:
you can enumerate to get the index:
var sum = [Int]()
for (index, _) in a.enumerated(){
sum.append(a[index] + b[index])
}
print(sum)// [5, 7, 9]
Is there a concise way in Swift of creating an array by applying a binary operation on the elements of two other arrays?
For example:
let a = [1, 2, 3]
let b = [4, 5, 6]
let c = (0..<3).map{a[$0]+b[$0]} // c = [5, 7, 9]
If you use zip to combine the elements, you can refer to + with just +:
let a = [1, 2, 3]
let b = [4, 5, 6]
let c = zip(a, b).map(+) // [5, 7, 9]
Update:
You can use indices like this:
for index in a.indices{
sum.append(a[index] + b[index])
}
print(sum)// [5, 7, 9]
(Thanks to Alexander's comment this is better, because we don't have to deal with the element itself and we just deal with the index)
Old answer:
you can enumerate to get the index:
var sum = [Int]()
for (index, _) in a.enumerated(){
sum.append(a[index] + b[index])
}
print(sum)// [5, 7, 9]
I am having trouble finding a matlab function to slice an element out of an array.
For example:
A = [1, 2, 3, 4]
I want to take out on element of this array, say the element 3:
B = [1, 2, 4]
Is there a matlab function for this or would I have to code the algorithm to construct a new array with all the elements of A except 3?
Do this:
index_of_element_to_remove = 3;
A(index_of_element_to_remove) = [];
now A will be [1 2 4]
If you want to remove more elements at the same time you can do:
index_of_element_to_remove = [1 3];
A(index_of_element_to_remove) = [];
now A will be [2 4]
By value, which will remove all elements equal to 3
A(find(A==3)) = []
Or by index
A(3) = []