Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Count each unique element in list of lists

16 views
Skip to first unread message

Yaşar Arabacı

unread,
Nov 8, 2013, 2:28:09 PM11/8/13
to pytho...@python.org
Hi,

I have a function that returns something like this;

[[[1, 5, 9], [2, 6, 7], [3, 4, 8]], [[1, 6, 8], [2, 4, 9], [3, 5, 7]]]

It is a list of list of lists. Each uppermost list is called a result.
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. I am looking for something like this;

for result in results:
print sum(1 for group in result for item in group)

[1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1]

Above code gives me 9,9. but I want the example output above.

I prefer a one liner because this is supposed to go into a doctest.
--
http://ysar.net/

yasar...@gmail.com

unread,
Nov 8, 2013, 2:38:10 PM11/8/13
to
This works;

>>> for result in results:
flat = list(item for group in result for item in group)
print [sum([1 for el in flat if el==current]) for current in flat]


[1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1]

But I am still open to suggestions if anyone thinks this can be improved.

Chris Angelico

unread,
Nov 8, 2013, 2:44:50 PM11/8/13
to pytho...@python.org
On Sat, Nov 9, 2013 at 6:28 AM, Yaşar Arabacı <yasar...@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

Vlastimil Brom

unread,
Nov 8, 2013, 6:00:41 PM11/8/13
to Yaşar Arabacı, python
2013/11/8 Yaşar Arabacı <yasar...@gmail.com>:
> Hi,
>
> I have a function that returns something like this;
>
> [[[1, 5, 9], [2, 6, 7], [3, 4, 8]], [[1, 6, 8], [2, 4, 9], [3, 5, 7]]]
>
> It is a list of list of lists. Each uppermost list is called a result.
> 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. I am looking for something like this;
>
> for result in results:
> print sum(1 for group in result for item in group)
>
> [1, 1, 1, 1, 1, 1, 1, 1, 1]
> [1, 1, 1, 1, 1, 1, 1, 1, 1]
>
> Above code gives me 9,9. but I want the example output above.
>
> I prefer a one liner because this is supposed to go into a doctest.
> --
> http://ysar.net/
> --
> https://mail.python.org/mailman/listinfo/python-list


Hi,
depending on the specification, the tests could be e.g.:
results = [[[1, 5, 11], [2, 6, 7], [3, 4, 8]], [[1, 6, 8], [2, 4, 9],
[3, 5, 7]]]

#testing for the sum 1..9 - this would also pass for some "paired"
repeated items, which most likely isn't indended
print all(sum((elem for sublist in result for elem in sublist))==45
for result in results)

# testing for different items without repeating - not only 1..9
print all(len(set(elem for sublist in result for elem in sublist))==9
for result in results)

# testing explicitely for items 0..9 in any order
print all((set(elem for sublist in result for elem in
sublist))==set([1,2,3,4,5,6,7,8,9]) for result in results)

hth,
vbr
0 new messages