From 1aa6fb14f2b157c41062c80ffaa1f67c1ba30198 Mon Sep 17 00:00:00 2001 From: Guo Lifeng <41283498+zichen2002@users.noreply.github.com> Date: Sun, 13 Jan 2019 10:39:28 +0800 Subject: [PATCH] Update example.py If the ignore_types is tuple instead of default (str, bytes), need add ignore_types argument when calling flatten() recursively. --- src/4/how_to_flatten_a_nested_sequence/example.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/4/how_to_flatten_a_nested_sequence/example.py b/src/4/how_to_flatten_a_nested_sequence/example.py index 5f69236..508f4b7 100644 --- a/src/4/how_to_flatten_a_nested_sequence/example.py +++ b/src/4/how_to_flatten_a_nested_sequence/example.py @@ -5,14 +5,14 @@ def flatten(items, ignore_types=(str, bytes)): for x in items: if isinstance(x, Iterable) and not isinstance(x, ignore_types): - yield from flatten(x) + yield from flatten(x, ignore_types) else: yield x -items = [1, 2, [3, 4, [5, 6], 7], 8] +items = [1, 2, [3, 4, (5, 6), 7], 8] # Produces 1 2 3 4 5 6 7 8 -for x in flatten(items): +for x in flatten(items, ignore_types=tuple): print(x) items = ['Dave', 'Paula', ['Thomas', 'Lewis']]