I have an array with ascii characters values like this
arryAsc[] = [97, 100, 97, 115, 100]
Now I want to make an array arrSplit[] and want to save these values in array like this.
arrSplit[] = [9, 7, 1, 0, 0, 9, 7, 1, 1, 5, 1, 0, 0]
If anyone can help thanks in advance.
You can simply do like this :
var a:Array = [97, 100, 97, 115, 100];
var b:Array = a.join('').split('');
trace(b); // gives : 9,7,1,0,0,9,7,1,1,5,1,0,0
Hope that can help.
Loop through the array, casting each element as a String, then use split() with an empty string as delimiter to break apart each string into an array of characters. You can use concat to combine all the smaller arrays back into your larger arrSplit array.
Related
I have a json data like :
[
[
"2020-05-07T16:30:00.000+0530",
1,
29,
693,
0,
7,
3663,
7413
],
[
"2020-05-07T15:30:00.000+0530",
0,
16,
996,
3,
13,
4452,
10106
]
]
Using JQ, I want to add the corresponding elements of the both array and result a new array. In case of date string the value from one of the array will be fine. The Expected output is
[
"2020-05-07T16:30:00.000+0530",
1,
45,
1689,
3,
20,
8115,
17519
]
Please can u suggest the solution?
Pair corresponding elements using transpose, and create a new array with sums of them.
transpose | [.[0][0]] + map(add)[1:]
demo at jqplay.org
I have two arrays:
a = [a_first_element, a_second_element, a_third_element, a_fourth_element]
b = [b_first_element, b_second_element, b_third_element, b_fourth_element]
I would like to insert in the first array, at even positions, elements of the second array.
So the final array shoud look like :
[a_first_element, b_first_element, a_second_element, b_second_element, a_third_element,b_third_element, etc]
The arrays are made of the same number of items (around 30)
How could I do that ?
It looks like you want to zip the arrays together. Doing this:
a = [1, 2, 3, 4]
b = [111, 222, 333, 444]
c = a.zip(b)
will set c to:
[[1, 111], [2, 222], [3, 333], [4, 444]]
which is almost what you want, but you probably don't want the nested arrays. To get rid of the nested arrays, just call flatten:
c = a.zip(b).flatten()
Now c is set to:
[1, 111, 2, 222, 3, 333, 4, 444]
I have one array like below:
Array = [21.2, 13.6, 86.2, 54.6, 76, 34, 78, 12, 90, 4];
Now I want to add Array values from the first index to the fourth index, and from the seventh index to the tenth.
I wrote this code but it did not work correctly.
s = 0
for I=1:10
if 1<=I<=4 | I>6
s = s + Array(I);
end
end
Please help me with this problem.
You can implement it without any kind of loop that may slow your code. To make those sums, you just need to use 'sum'. For further help, please read this. In your case, I'd do the following:
a = [21.2, 13.6, 86.2, 54.6, 76, 34, 78, 12, 90, 4];
b = sum(a(1:4))+sum(a(8:end));
Let's assume a multi-dimensional array
import numpy as np
foo = np.random.rand(102,43,35,51)
I know that those last dimensions represent a 2D space (35,51) of which I would like to index a range of rows of a column
Let's say I want to have rows 8 to 30 of column 0
From my understanding of indexing I should call
foo[0][0][8::30][0]
Knowing my data though (unlike the random data used here), this is not what I expected
I could try this that does work but looks ridiculous
foo[0][0][[8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],0]
Now from what I can find in this documentation I can also use
something like:
foo[0][0][[8,30],0]
which only gives me the values of rows 8 and 30
while this:
foo[0][0][[8::30],0]
gives an error
File "<ipython-input-568-cc49fe1424d1>", line 1
foo[0][0][[8::30],0]
^
SyntaxError: invalid syntax
I don't understand why the :: argument cannot be passed here. What is then a way to indicate a range in your indexing syntax?
So I guess my overall question is what would be the proper pythonic equivalent of this syntax:
foo[0][0][[8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],0]
Instead of
foo[0][0][8::30][0]
try
foo[0, 0, 8:30, 0]
The foo[0][0] part is the same as foo[0, 0, :, :], selecting a 2d array (35 x 51). But foo[0][0][8::30] selects a subset of those rows
Consider what happens when is use 0::30 on 2d array:
In [490]: np.zeros((35,51))[0::30].shape
Out[490]: (2, 51)
In [491]: np.arange(35)[0::30]
Out[491]: array([ 0, 30])
The 30 is the step, not the stop value of the slice.
the last [0] then picks the first of those rows. The end result is the same as foo[0,0,0,:].
It is better, in most cases, to index multiple dimensions with the comma syntax. And if you want the first 30 rows use 0:30, not 0::30 (that's basic slicing notation, applicable to lists as well as arrays).
As for:
foo[0][0][[8::30],0]
simplify it to x[[8::30], 0]. The Python interpreter accepts [1:2:3, 0], translating it to tuple(slice(1,2,3), 0) and passing it to a __getitem__ method. But the colon syntax is accepted in a very specific context. The interpreter is treating that inner set of brackets as a list, and colons are not accepted there.
foo[0,0,[1,2,3],0]
is ok, because the inner brackets are a list, and the numpy getitem can handle those.
numpy has a tool for converting a slice notation into a list of numbers. Play with that if it is still confusing:
In [495]: np.r_[8:30]
Out[495]:
array([ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29])
In [496]: np.r_[8::30]
Out[496]: array([0])
In [497]: np.r_[8:30:2]
Out[497]: array([ 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])
How can I sort a region in an array in Scala?
I have an array, say var DivA = new Array[Int](100005).
I filled the elements in DivA up to some position which is less than 100005.
For example my array looks like Diva = {1,2,10,5,15,20}.
In C++ we have sort(DivA, DivA + N), but how can we sort arrays in Scala up to a certain index?
I tried Sorting.quickSort(DivA), but I don't know how to define the index up to which I want to sort the array, so that the statement above sorts the array, and the array looks like DivA={0,0,0,0,0}?
var Diva=new Array[Int](10000);
for(j<-15 to 0 by -1){
DivA(posA)=j;
posA=posA+1;
}
Sorting.quickSort(DivA);
for(j<-0 to posA-1)
{
print(DivA(j));
print(" ");
}
If you want to sort a region in an array, you can use the sort method in java.util.Arrays:
java.util.Arrays.sort(DivA, 0, 15)
this method offers fromIndex and toIndex. Note that this method sorts in place. That's probably OK in your case, as the style you apply seems to be "imperative".
If performance matters, arrays will probably be faster than other collections. Other reasons for using arrays might be memory consumption or interoperability.
Otherwise you can probably design your data structures/algorithm in a different (more "functional") way by using idiomatic Scala collections.
You can use sorted or sortBy, depending on your needs. In case of simple Array[Int], sorted should be enough:
val arr = Array.iterate(15, 15)(_ - 1)
arr: Array[Int] = Array(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
val sortedArray = arr.sorted
sortedArray: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
You can provide your own Ordering typeclass if you want some other kind of order.
You can find it in the documentation.
Although, as was noted in other answer, you should prefer immutable Vector unless you have good reason to switch to Array.
sorted() method is common for all collections, and Arrays in Scala are implicitly converted to a collection.
So you can call sorted on Array[Int], eg:
scala> val p = (10 to 1 by -1).toArray
p: Array[Int] = Array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
scala> p.sorted
res5: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
First, if you are programming in Scala, you should think about Vector or List because those are immutable which is prefferd way of programming in Scala...
And for Vector and List there is sorted() function which sorts an array