List groups the elements of another list

yuri1969 shared this question 6 years ago
Answered

Please help in the following case.

I have a list consisting of the numbers. I want it to create a list of lists grouping identical items of the first list.

For example, to the list {1,1,1,2,2,1,3,3,1,1} I would like to receive a list {{1,1,1},{2,2},{1},{3,3},{1,1}}.

Is there a formula that allows get this result?

Comments (4)

photo
1

Hi, with few steps.

Cheers.

Michel

https://ggbm.at/562695

photo
1

Hi,

another solution ... longer!


Patrick.

https://ggbm.at/562697

photo
1

Using the solutions proposed by miir found even shorter solution.

Let lista1 divided list.

lista2=RemoveUndefined[Sequence[If[Element[lista1, i] ≠ Element[lista1, i + 1], i], i, 1, Length[lista1]]]

lista3=Sequence[If[i ≟ 1, Element[lista2, i], Element[lista2, i] - Element[lista2, i - 1]], i, 1, Length[lista2]]

lista4=Sequence[Sequence[Element[lista1, Element[lista2, i]], j, 1, Element[lista3, i]], i, 1, Length[lista2]]


In lista4 have is what I need.


Thanks to all for your help

photo
1

Hi all,


Based on the principle of miir and Noel, another solution in 2 lines

    list2 = RemoveUndefined[Sequence[If[Element[list1, n - 1] ≠ Element[list1, n], n], n, 1, Length[list1] + 1]]

    list3 = Sequence[Take[list1, Element[list2, n], Element[list2, n + 1] - 1], n, 1, Length[list2] - 1]

Raymond

https://ggbm.at/562711

© 2019 International GeoGebra Institute