1
$\begingroup$

Is there a way to take equal elements out of a list and their position???

An example can be

v = {13.51, 13.51, 12.36, 12.36, 12.36, 12.36, 12.85, 12.85, 12.85, 12.85}

with desired output a list like:

 {{13.51, 12.36, 12.85},{2,4,4}}

where on the left there are the elements written with the correct order and on the right how many times they repeat. The elements in the starting list are considered to be ordered. Thanks

$\endgroup$
1
  • 1
    $\begingroup$ Does Tally[v]//Transpose not do what you want? If not, perhaps you could post the expected outcome for v and v = {1, 3, 3, 3, 4, 4, 4}? $\endgroup$ – user1066 Sep 17 '20 at 9:34
2
$\begingroup$

Try

v = {13.51, 13.51, 12.36, 12.36, 12.36, 12.36, 12.85, 12.85, 12.85, 12.85};
Transpose[Tally[v]]

which returns

{{13.51, 12.36, 12.85}, {2, 4, 4}}

and see if that works in every case for you. Approximate decimal numbers may sometimes cause problems when testing for equality.

$\endgroup$
0
$\begingroup$

Try Union

{13.51, 13.51, 12.36, 12.36, 12.36, 12.36, 12.85, 12.85, 12.85, 12.85} // Union
(*{12.36, 12.85, 13.51}*)

{1, 3, 3, 3, 4, 4, 4} //Union
(*{1, 3, 4}*) 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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