Count each unique element in list of lists

Chris Angelico rosuav at gmail.com
Fri Nov 8 20:44:50 CET 2013


On Sat, Nov 9, 2013 at 6:28 AM, Yaşar Arabacı <yasar11732 at gmail.com> wrote:
> I want to write a code that
> shows that each elem in sublists of result on appears once in whole
> sublist in order to add it to
> my doctest.

So, to clarify the problem: You want to ensure that every element
occurs exactly once, neither more nor less? And you have a guarantee
that the lists and sublists have a specific nesting depth? Try this:

[sorted((x for l in result for x in l)) for result in results]

Flattens and sorts the lists.

[sorted((x for l in result for x in l))==list(range(1,10)) for result
in results]

Flattens, sorts, and compares with a template list (which in this case
is [1, 2, 3, 4, 5, 6, 7, 8, 9] from range()).

ChrisA



More information about the Python-list mailing list