I am converting a large Python code into Julia. In my Python code, I have the ndarray datatype objects that
- allow me to add fieldnames to each value of an array (as in the name of a chemical species and its concentration) as well as specify its format (which is all Float64, so it does not really matter as they are all the same type), and
- these fieldnames are like keys and can be used to retrieve the values they are associated with. So if I want H2O, it retrieves a particular species’ (H2O) concentration.
- allows me to initialize a complex object (i.e., 2D arrays of these objects that have 20 species each with their chemical name) like with the zeros and empty functions in python.
In python, I would do this:
dtype1=dict(names=listofnames,formats=[np.float64*len(listofnames)]
N = np.zeros((10,10),dtype=dtype1)
- What would be the exact replacement type object for this in Julia?
- Are there any modules that I should be aware of that help me create and manipulate such objects?
So far, I have created a mutable struct - say, Species, created a constructor for default values, an ArrayofStructs as ArrayofSpecies, and used this object to fill a 2D array.
mutable struct Species
name::String
value::Float64
end
Species(x::String)=Species(x,0.0)
ArrayofSpeciesObject = [Species(x) for x in arrayofstrings]
A=fill(arrayofSpeciesObject,10,10)
But I am missing the ease of retrieving values for each chemical species with their fieldnames in python as you would for dictionaries, i.e., A[i,j]["H2O"]
Is there a better way to do this?
Thanks!!
6 posts - 3 participants