FW: ciao

Raymond Hettinger python at rcn.com
Sun Mar 27 02:23:37 EDT 2011


On Mar 26, 11:34 am, MRAB <pyt... at mrabarnett.plus.com> wrote:
> On 26/03/2011 18:07, bledar seferi wrote:
>
> >     3.Scrivere unafunsioncheprende comeargomentouna lista
> >     diinterierestituisce uninsieme contenentequei numerichesono2 o più
> >     voltenellalista fornita.Per esempio,seservecomelista di
> >     input=[1,2,3,3,4,4,5,6,7,7,7],quindila funzionerestituiràilset([3,4,7])
>
> >     Mi puoi aiutare a fare questo programma a python
>
> >     Grazie
>
> I'd use a defaultdict to count them.

Or use itertools.groupby() to group consecutive values, count the
length of each group, and emit the value whenever the count is more
than one:

>>> s = [1,2,3,3,4,4,5,6,7,7,7]
>>> [k for k, g in groupby(s) if len(list(g)) > 1]
[3, 4, 7]


Raymond



More information about the Python-list mailing list