Defining a map to arrays containing slices in Golang - arrays

In Go (golang), is it possible to define a map from strings to arrays, and in each array element I want to store a slice. Like this:
var data = make(map[string][2]Slice[]float64)
Then I want to retrieve my data, something like this:
floatValue0 = data["string-key"][0][#]
floatValue1 = data["string-key"][1][#]

data := map[string][2][]float64{"golang": {[]float64{3.14, 3.15}, []float64{3.12, 3.16}}}
fmt.Println(data["golang"][0][0])
output:
3.14

Related

how i can use ListOfArrays in Kotlin

I have been working mainly in JAVA but now I need to program some small things in kotlin. Among other things I am trying to convert the result of a database query into a list of array. The result of the database query has 4 columns, the number of rows I can not predict.
I have tried the following:
var output: mutableList<List<String>>
var output = mutableListOf<String>()
var output = mutableListOf<ArrayList>
List<List<String>> listOfLists = new ArrayList<List<String>>()
What I would like to do is this:
output.add(arrayOf("Field1", "Filed2", "Field3", "Field4"))
It can't be that hard, can it?
A list of arrays can be expressed as List<Array<T>>.
So if you want a mutable list to which you can add arrays of strings, simply do:
var output = mutableListOf<Array<String>>()
output.add(arrayOf("Field1", "Filed2", "Field3", "Field4"))
That being said, why do you want to use arrays? It's generally more convenient to work with lists, unless you're constrained by another API.
In kotlin you should use lists where you can. What you are trying to create is:
val output = mutableListOf<List<String>>()
output.add(listOf("Field1", "Filed2", "Field3", "Field4"))
If you are iterating through some other list to create your data for output you could do something like:
val otherList = listOf<String>("a", "b", "v")
val output = otherList.map { otherListData ->
listOf(otherListData + 1, otherListData + 2, otherListData + 3, otherListData + 4)
}
In which case you would only have immutable lists.

Exchange different arrays elements in ruby

Is there any ruby method that I can use to replace two different arrays elements?
For instance, I have these two arrays:
#Before exchange
arr_one = [1,2,3,4,5]
arr_two = ["some", "thing", "new"]
After replacements the elements I am expecting something like this:
#After exchange
arr_one = ["some", "thing", "new"]
arr_two = [1,2,3,4,5]
How can I handle this with or without a ruby method?
You mean, you want to 'exchange' values of local variables? It's quite easy in Ruby:
arr_one, arr_two = arr_two, arr_one
If you need to have the arrays stay put (i.e. the values of the variables not change) and exchange the contents, then this should do:
arr_tmp = arr_one.dup
arr_one.replace(arr_two)
arr_two.replace(arr_tmp)

how to get array value by using index value of another value?

let nameArray = ["ramesh","suresh","rajesh"]
let idArray = ["100","101","102"]
Now i want value of "idArray" by using index value of "nameArray".
if nameArray index is 0. Output is 100
In Object Oriented Programming, objects should own their properties. So instead of having two data structures describe the same object, either use structs like Mr. Vadian has suggested, or have one array store all the properties of the objects:
let zippedArray = Array(zip(nameArray, idArray))
And now to get the object in a given index, you can use the following:
let index = 0
let element = zippedArray[0]
print(element.0) //ramesh
print(element.1) //100

Creating 2 D arrays dynamically in Perl

I want to create 2-d arrays dynamically in Perl. I am not sure how to do this exactly.My requirement is something like this-
#a =([0,1,...],[1,0,1..],...)
Also I want to name the references to the internal array dynamically. i.e. I must be able to refer to my internal arrays with my chosen names which I will be allocating dynamically.
Can someone please help me on this.
It sounds like you want a tree/hash of arrays. Use references to achieve this.
Example of hash of array of array:
$ref = {};
$ref->{'name'} = [];
$ref->{'name'}[0] = [];
$ref->{'name'}[0][1] = 3;
This could be dynamic if required. Make sure you initialise what the reference is pointing at.
Example array of array references:
my #x;
$x[$_] = [0..int(rand(5)+1)] for (0..3);
You probably have some kind of loop?
for (...) {
my #subarray = ...;
push #a, \#subarray;
}
You could also do
for (...) {
push #a, [ ... ];
}
If it's actually a foreach loop, you could even replace it with a map.
my #a = map { ...; [ ... ] } ...;

iPython numpy - How to change value of an array slice with a map

I've got a 3-dim array [rows][cols][3] with values between 0 and X.
I need to manipulate a specific dimension in the array. So I've taken a slice of the part I want to manipulate
arr_slice = array[:,:,0]
now I can make some manipulations like arr_slice *= 3 and that will change the original array, as I intended.
However, I need to change values according to a map, which is an array with size X that maps the values of the slice (0-X) to new values. the map is called mapping
so I know mapping[arr_slice] will do what I want, but using it like this:
arr_slice = mapping[arr_slice]
will of course change only arr_slice and not the original array I have.
So, How can I perform this task to change the original array?
The array is actually an image, that I'm trying to manipulate it's Y values in YIQ format:
im_eq = np.copy(im_orig)
if (rgb):
im_eq = rgb2yiq(im_eq)
im = im_eq[:,:,0]
else:
im = im_eq
mapping = get_cumutative_histogram(im)
im = mapping[im.astype(int)] # the problematic line
You need to address the slice elements:
im[:] = mapping[im.astype(int)]
for example:
from pylab import *
a = rand(10)
sl = a[4:9]
print sl # ->: array([ 0.97278179, 0.7894741 , 0.38051133, 0.42684762, 0.82670638])
sl[:] = 1
print a #-> array([ 0.21125781, 0.4235981 , 0.81950229, 0.93937973, 1. , 1. , 1. , 1. , 1. , 0.39047808])

Resources