A problem that arises more often than I care for is that I want to modify specific elements nested lists.
Examples may be wanting to add 1
to the second value, get the Sqrt
of the third or wanting to add/change the Unit
of the penultimate one. My current workaround is using Table
to do so, which is not only untidy, but probably pretty inefficient, too. (I just downloaded a list that's 190k lines long, with 18 values a line. I'm not even gonna try that)
test = Table[{x, x + 1, x + 2, x + 3, x + 4, x + 5}, {x, 0, 10}];
Table[{test[[i, 1]], test[[i, 2]] + 1, Sqrt[test[[i, 3]]], Quantity[test[[i, 4]], "Meters"], test[[i, 5]], test[[i,6]]}, {i, 1, Length[test]}]
what I'd very much prefer is using Replace all /.
rule=**???**
test /. rule
I as I don't want to change every third element to a "static" value or something, I assume that I'd need to use Slots (#
), but anything I can come up with like
rule = #[[2]] -> #[[2]] + 1
rule = #2 -> #2 + 1
will throw me an error. I'm sure there's a solution, and I'm sure more experienced people will look at this and see my obvious mistake (and lack of understanding of Slots), but I don't know, and would appreciate any suggestions.
Edit: I didn't intentionally drop the last element, and fixed it.