2013/11/8 Yaşar Arabacı <yasar11...@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 -- https://mail.python.org/mailman/listinfo/python-list