Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Julia equivalent for an ndarray datatype object in python

$
0
0

I am converting a large Python code into Julia. In my Python code, I have the ndarray datatype objects that

  1. 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
  2. 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.
  3. 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)

  1. What would be the exact replacement type object for this in Julia?
  2. 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

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles