Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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$. – Rahul Aug 8 '13 at 2:00
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? – Mr.Wizard Aug 8 '13 at 2:06
@RahulNarain Ahhh... I'm so sorry. – Niniar Aug 8 '13 at 2:06
@Mr.Wizard I have updated the example, terrible mistake. – Niniar Aug 8 '13 at 2:07
@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? – Mr.Wizard Aug 8 '13 at 2:10
up vote 2 down vote accepted

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}
share|improve this answer

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} *)
share|improve this answer
Wait-- I really like the style but I didn't make the assumption that the list is monotonically increasing. – Mr.Wizard Aug 8 '13 at 3:11
You know, I'm being too much of a nitpicker. It's a good answer, just not the same behavior as mine. +1 (again) – Mr.Wizard Aug 8 '13 at 3:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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