Replace value in association data-set

Kiril Danilchenko 06/13/2018. 1 answers, 82 views
performance-tuning associations

I have an association dataset with a depth of two (each value is an association too, and the size of the dataset ~ 30000 keys and an average size of values are ~2000 keys association). The dataset looks like:

  dataset=<| 2 -> <|3 -> 3, 5 -> 4|>,4 -> <|5 -> 15|>, 5 -> <|1 -> 41, 2 -> 
   4,3 -> 10, 4 -> 11|>|>

I need to change specific values in this dataset. I do it in the following manner:

dataset= 
 ReplacePart[
   dataset, {Key@23980, Key@19969} -> {5, 
  6}]; // AbsoluteTiming

  {0.000110522, Null}

This operation takes a lot of time for my dataset, around 110 msec. I repeat this procedure thousands of times, so it is a real bottleneck for my program.

Any suggestions on how to speed up this procedure?

Here is a working example:

dataset = <|2 -> <|3 -> 3, 5 -> 4|>, 4 -> <|5 -> 15|>, 5 -> <|1 -> 41, 2 -> 4, 3 -> 10, 4 -> 11|>|>;
dataset = ReplacePart[dataset, {Key@4, Key@5} -> {5, 6}]
(* <|2 -> <|3 -> 3, 5 -> 4|>, 4 -> <|5 -> 15|>, 5 -> <|1 -> 41, 2 -> 4, 3 -> 10, 4 -> 11|>|> *)
(* <|2 -> <|3 -> 3, 5 -> 4|>, 4 -> <|5 -> {5, 6}|>, 5 -> <|1 -> 41, 2 -> 4, 3 -> 10, 4 -> 11|>|> *)

1 Answers


Carl Woll 06/13/2018.

As the last basic example for Association says:

Associations can be modified by resetting values:

assoc=<|a->x,b->y,c->z|>

<|a -> x, b -> y, c -> z|>

assoc[b]=w

w

assoc

<|a -> x, b -> w, c -> z|>

So, you can just set the values directly using:

dataset = <|
    2 -> <|3 -> 3, 5 -> 4|>,
    4 -> <|5 -> 15|>,
    5 -> <|1 -> 41, 2 -> 4,  3 -> 10, 4 -> 11|>
|>;
dataset[4, 5] = {5, 6};

dataset

<|2 -> <|3 -> 3, 5 -> 4|>, 4 -> <|5 -> {5, 6}|>, 5 -> <|1 -> 41, 2 -> 4, 3 -> 10, 4 -> 11|>|>

Notice that 4, 5 has been reset from 15 to {5, 6}.


HighResolutionMusic.com - Download Hi-Res Songs

1 BLACKPINK

Kiss And Make Up flac

BLACKPINK. 2018. Writer: Soke;Kny Factory;Billboard;Chelcee Grimes;Teddy Park;Marc Vincent;Dua Lipa.
2 Martin Garrix

Waiting For Tomorrow flac

Martin Garrix. 2018. Writer: Pierce Fulton;Mike Shinoda;Martijn Garritsen;Brad Delson.
3 John Legend

Written In The Stars flac

John Legend. 2018. Writer: Kiana Brown;Santoy;Kevin White;Mike Woods;MZMC;The Heavy Group;Rice N' Peas.
4 Martin Garrix

Access flac

Martin Garrix. 2018. Writer: Martin Garrix.
5 Martin Garrix

Yottabyte flac

Martin Garrix. 2018. Writer: Martin Garrix.
6 Alan Walker

Diamond Heart flac

Alan Walker. 2018. Writer: Alan Walker;Sophia Somajo;Mood Melodies;James Njie;Thomas Troelsen;Kristoffer Haugan;Edvard Normann;Anders Froen;Gunnar Greve;Yann Bargain;Victor Verpillat;Fredrik Borch Olsen.
7 Cardi B

Taki Taki flac

Cardi B. 2018. Writer: Bava;Juan Vasquez;Vicente Saavedra;Jordan Thorpe;DJ Snake;Ozuna;Cardi B;Selena Gomez.
8 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
9 Post Malone

Sunflower flac

Post Malone. 2018. Writer: Louis Bell;Billy Walsh;Carter Lang;Swae Lee;Post Malone.
10 Dyro

Latency flac

Dyro. 2018. Writer: Martin Garrix;Dyro.
11 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
12 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora.
13 Zara Larsson

Ruin My Life flac

Zara Larsson. 2018. Writer: Delacey;Michael Pollack;Stefan Johnson;Jordan Johnson;Sermstyle;Jackson Foote.
14 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
15 ZAYN

Fingers flac

ZAYN. 2018. Writer: Zayn Malik;Alex Oriet;David Phelan.
16 Dewain Whitmore

Burn Out flac

Dewain Whitmore. 2018. Writer: Dewain Whitmore;Ilsey Juber;Emilio Behr;Martijn Garritsen.
17 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
18 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
19 Blinders

Breach (Walk Alone) flac

Blinders. 2018. Writer: Dewain Whitmore;Ilsey Juber;Blinders;Martin Garrix.
20 Julia Michaels

There's No Way flac

Julia Michaels. 2018. Writer: Ian Kirkpatrick;Justin Tranter;Julia Michaels;Lauv.

Related questions

Hot questions

Language

Popular Tags