Given array=[1, 2, 3, 4, 5, 6]
I want to choose the 0-th 2-nd, 4-th index value to build a new array
array1=[1, 3, 5]
Could someone show me how to do using python? Thanks~
If it is just 0, 2, and 4, you can use operator.itemgetter():
from operator import itemgetter
array1 = itemgetter(0, 2, 4)(array)
That will be a tuple. If it must be a list, convert it:
array1 = list(itemgetter(0, 2, 4)(array))
If the point is to get the even numbered indices, use slicing:
array1 = array[::2]
Whichever you are looking for, you could use a list comprehension:
array1 = [array[i] for i in (0, 2, 4)]
or
array1 = [array[i] for i in xrange(0, len(array), 2)]
You can try something like this. In python the nth term of a list has index (n-1). Suppose the first element you want is 2, which happens to be the element 1 of array. Just save the first element index in a variable. Append it to the new list array1 and increase the index by 2. Continue doing this until the list array is exhausted.
from numpy import*
array=[1,2,3,4,5,6]
array1=[]
term=1
while term<len(array): # if the array length is 6 then it will have upto element 5.
array1.append(array[term])
term=term+2 # 2 is the gap between elements. You can replace it with your required step size.
Related
Given an array [2,4,6,7]. We need to choose one number x. Over iterating entire array we need to assign the new values a[i]=a[i]/x.
In the above scenario they choose 2.
The result array is [1,2,3,7]
The array cost is 1+2+3+7=13.
How can we choose an element randomly?
There is a module in python called
random and that module has a function random.choice(<array>)
so the code would be :
array = [2, 4, 6, 7]
x = random.choice(array)
for i in range(len(array))
array[i] /= x
I am trying to create a method that takes an array as its argument and multiplies the number in the array to the arrays Index. When there is an element that is not a number i want to skip over it, and do not include it in the new array
E.g. add_index_to_array([1, 2, 3, 4, 5]) should return [0, 2, 6, 12, 20]
E.g. add_index_to_array(["A", 32, true, "B", 5.3, 0]) should return [32, 21.2, 0]
def add_index_to_array (array)
new_array = array.map.with_index { |i,a| i * a}
#if my array includes anything but a number return an empty array
end
puts add_index_to_array(['a',2,3,4])
# ["", 2, 6, 12]
Following what you've tried until now, you can use next to pass to the next element in the array during the iteration unless the element is an Integer:
array.map.with_index do |i,a|
next unless i.is_a?(Integer)
i * a
end
That will leave nil values after an element is skiped, so you can use compact after all to remove them.
array.map.with_index do |i,a|
next unless i.is_a?(Integer)
i * a
end.compact
But really there are many ways. The easiest to understand (for me) - is to use each_with_object plus with_index, so you can get three objects during the iteration; the current element, an accumulator and the index. This way you can push to the accumulator the product of the current index and the current element only if the current element is an Integer:
array.each_with_object([]).with_index do |(e, array), index|
array << index * e if e.is_a?(Integer)
end
Looking to your last example, it's clear you should be checking against Numeric objects instead of just Integer ones, since 5.3 isn't an Integer.
Input
a = ["A", 32, true, "B", 5.3, 0,6]
Code
a.select { |x| x.is_a?(Numeric) }
.map
.with_index { |val, i| val * i }
#=>[0, 5.3, 0, 18]
I checked against Numeric class because your number could be Integer or Float.
I have
arr = [6, 5, 4, 3, 2, 1]
I want to use the ...5, 4, 3, 2, 1] part of the array in a recursive call while keeping the 6 in the array (at its current position) for future use.
It feels very similar to pointer arithmetic in C, I'm just not sure how to implement something like that in Python (ver 3.7). I'm lost as to how to preserve the 6 in the array at it's position, which is essential as the array needs to be maintained in sorted descending order.
Any guidance on how to get around this is appreciated.
You can access the elements of the arr list in the following manner while not disturbing the elements of it
>>> arr[2:]
[4, 3, 2, 1]
>>> arr[1:]
[5, 4, 3, 2, 1]
>>> arr
[6, 5, 4, 3, 2, 1]
>>> arr[2:4]
[4, 3]
List indexing doesn't affect the elements inside. I hope this answers your question
You can use a part of an array in a recursive call without changing the original array using start and end index.
arr=[6,5,4,3,2,1], now if you want to use arr from index=1 then just pass
start and end i.e, fun(arr, start, end) in the function fun where, start=1 and end=length-1, this will not modify the original array and at the same time you can use array from start to end in recursive calls.
When parsing CSV file I need to combine fields of a row into an array starting from 4th field (3rd array element). I want to manipulate each row as in example below:
Original array:
array1 = [1,2,3,4,5]
Changed array:
array2 = [1,2,3,[4,5]]
My code is here:
array1[0..2].push(array1[3..array1.length])
=> [1, 2, 3, [4, 5]]
My question is: Is there a better/cleaner/simpler way to convert part of an array into subarray?
There is! You can do this
a = a[0..2] + [a[3..-1]]. In ruby + can be used to concatenate arrays. Additionally, n..-1 gives you elements n to the end of the array. As a caveat, + is slower and more expensive than concat so if you were to do a[0..2].concat([a[3..-1]) it will be cheaper and faster.
This is the array:
array = [ 1, 2, 3, [4, 5, 6] ]
Can I use "delete_at" method to delete the "5"?
array.delete_at[x] method
What would the correct syntax be?
Your 'array' has only 4 elements. If it's subarray you probably should do something like that
array[3].delete_at(1)
to delete the second element of subarray that's a fourth element of 'array' array.
Welcome to Stack Overflow!
This one is longer and less efficient but it allows you to select the item to be deleted by value instead of by position (array index). That's useful when you don't know the position.
array.map {|x| x.delete(5) if x.instance_of?(Array); x}