入れ子リストの中身を順に表示
問題文からいまいち仕様が読み取りにくい。こんなのでいいのかな。
import operator def flatten(obj): """ flatten specified nested list. >>> flatten([1, [2, 3, 4], 5, [[6], [7, 8], 9], 10]) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] """ if isinstance(obj, list): return reduce(operator.add, (flatten(elem) for elem in obj), []) else: return [obj] def print_atoms(obj): """ print conent of ntested list >>> print_atoms([1, [2, 3, 4], 5, [[6], [7, 8], 9], 10]) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 """ print ', '.join(str(elem) for elem in flatten(obj)) def is_all_int(obj): """ return if specified object consists of int and list only. >>> is_all_int([1, 2, [], 3]) True >>> is_all_int([1, '2', [3]]) False """ return all(isinstance(e, int) for e in flatten(obj)) def _test(): import doctest doctest.testmod() if __name__ == '__main__': _test()