I have the following data:
data =
{{{1931, 1, 1}, 7.78}, {{1931, 1, 2}, 5.}, {{1931, 1, 3}, "x"},
{{1931, 1, 4}, "x"}, {{1931, 1, 16}, 5.`}, {{1931, 1, 17}, 3.89`},
{{1931, 1, 18}, "x"}, {{1931, 1, 20}, 2.22`}};
I want to replace the "x"-values with the last preceding number:
data //. {a___, b : {_, c_}, {d_, "x"}, e___} :> {a, b, {d, c}, e}
gives the expected result:
{{{1931, 1, 1}, 7.78}, {{1931, 1, 2}, 5.}, {{1931, 1, 3}, 5.}, {{1931,1, 4}, 5.}, {{1931, 1, 16}, 5.}, {{1931, 1, 17}, 3.89}, {{1931, 1, 18}, 3.89}, {{1931, 1, 20}, 2.22}}
but becomes very slow as elements grow: $4.6$ seconds with $10000$ elements and seemingly forever with $25000$. My lists are long ($25000+$ elements) but have very few ($50$ - $100$) "x"-values.
I have no idea how a fast functional solution could look like.
g[v : {s_List, n_?NumericQ}] := (temp = n; v); g[v : {s_List, "x"}] := {s, temp}; g /@ data
$\endgroup$