Message Boards Message Boards

1
|
9561 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

How To Convert a List of Rules to an Association

Posted 10 years ago

I am pulling data from an XML file that I don't control the format of. I want to place its data in a list of associations, with the end goal of producing a dataset. I was delighted to discover that this works...

xmlData = 
  XMLElement[
   "Configuration", {"ActionPotentialBase" -> "0", 
    "FiringWeight" -> "1", "FiringThreshold" -> "5", 
    "GroupsPerModel" -> "5", "NeuronsPerGroup" -> "50", 
    "IntraGroupConnectionsPerNeuron" -> "5", 
    "ExtraGroupConnectionsPerGroup" -> "50", 
    "NeuralNetModelRunMethod" -> "Recursive"}, {}];
Apply[Association, xmlData[[2]]]

...returning an association in pretty much a single line of code.

The problem is, the XML file defines most of these values as strings, and I am going to use them downstream as integers. Is there some elegant way of casting them to integers quickly and easily, or am I forced to resort to something long-winded like...

<| "ActionPotentialBase" -> 
  ToExpression["ActionPotentialBase" /. xmlData[[2]]],
 ... and so on ..,
  |>

Thanks in advance.

Brad

POSTED BY: Brad Varey

Try this

data = Query[ToExpression@# &]@Apply[Association, xmlData[[2]]];

I assume you are not interested in using your last key.

In[62]:= NumericQ /@ Values[data]
Out[62]= {True, True, True, True, True, True, True, False}
POSTED BY: Emerson Willard
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract