3
$\begingroup$

I have a list of the form:

list = {1,1,1,2,2,2,3,3,3,4,4,4,7,7,7,19,19,19};

Where we can see that there are an ordered set of integer values that repeat themselves some fixed number of times N. Is there a nice one-liner to transform this list into something like:

transformedList = {1,1+1/3,1+2/3,2,2+1/3,2+2/3,3,3+1/3,3+2/3,4,4+1/3,4+2/3,7,7+1/3,7+2/3,19,19+1/3,19+2/3}

Where we essentially choose each new element we come by in the list as an anchorpoint, and replace the repeats of that element as equispaced points along a line the anchor integer value and the next higher integer?

$\endgroup$
8
  • $\begingroup$ You say "equispaced points along a line between each successive pair of anchor points", but you transform $4,4,4,7$ to $4,4+\frac13,4+\frac23,7$ instead of $4,5,6,7$. $\endgroup$
    – user484
    Commented Aug 8, 2013 at 2:00
  • $\begingroup$ Rahul is right; something doesn't add up here. If your sample output is really what you want we should rephrase this question to match. Is it? $\endgroup$
    – Mr.Wizard
    Commented Aug 8, 2013 at 2:06
  • $\begingroup$ @RahulNarain Ahhh... I'm so sorry. $\endgroup$
    – Niniar
    Commented Aug 8, 2013 at 2:06
  • $\begingroup$ @Mr.Wizard I have updated the example, terrible mistake. $\endgroup$
    – Niniar
    Commented Aug 8, 2013 at 2:07
  • $\begingroup$ @Niniar Since I already answered your original question would you mind posting a new one with the revised information, and restoring this one to the original? $\endgroup$
    – Mr.Wizard
    Commented Aug 8, 2013 at 2:10

2 Answers 2

2
$\begingroup$

Here's the first thing that comes to mind:

# + Most@Range[0, 1, 1/Length@#] & /@ Split[list] // Flatten
{1, 4/3, 5/3, 2, 7/3, 8/3, 3, 10/3, 11/3, 4, 13/3, 14/3, 7, 22/3, 23/3, 19, 58/3, 59/3}
$\endgroup$
1
$\begingroup$

Basically the same approach as MrWizard but a bit shorter and using (except Range) different functions

Join @@ (# + Range[0, 1 - 1/#2, 1/#2] & @@@ Tally[list])

(* {1,4/3,5/3,2,7/3,8/3,3,10/3,11/3,4,13/3,14/3,7,22/3,23/3,19,58/3,59/3} *)
$\endgroup$
2
  • $\begingroup$ Wait-- I really like the style but I didn't make the assumption that the list is monotonically increasing. $\endgroup$
    – Mr.Wizard
    Commented Aug 8, 2013 at 3:11
  • $\begingroup$ You know, I'm being too much of a nitpicker. It's a good answer, just not the same behavior as mine. +1 (again) $\endgroup$
    – Mr.Wizard
    Commented Aug 8, 2013 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.