|
py 中有时候需要展平一个嵌套的列表,偶然发现一个已经实现的工具,位于:parlse.utils.py
- from typing import Any, Iterable, Iterator, List
- def flatten(x: Iterable[Any]) -> List[Any]:
- """flatten(sequence) -> list
- Returns a single, flat list which contains all elements retrieved
- from the sequence and all recursively contained sub-sequences
- (iterables).
- Examples:
- >>> [1, 2, [3,4], (5,6)]
- [1, 2, [3, 4], (5, 6)]
- >>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, (8,9,10)])
- [1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]
- >>> flatten(["foo", "bar"])
- ['foo', 'bar']
- >>> flatten(["foo", ["baz", 42], "bar"])
- ['foo', 'baz', 42, 'bar']
- """
- return list(iflatten(x))
- def iflatten(x: Iterable[Any]) -> Iterator[Any]:
- """iflatten(sequence) -> Iterator
- Similar to ``.flatten()``, but returns iterator instead"""
- for el in x:
- if _is_listlike(el):
- yield from flatten(el)
- else:
- yield el
- def _is_listlike(x: Any) -> bool:
- """
- >>> _is_listlike("foo")
- False
- >>> _is_listlike(5)
- False
- >>> _is_listlike(b"foo")
- False
- >>> _is_listlike([b"foo"])
- True
- >>> _is_listlike((b"foo",))
- True
- >>> _is_listlike({})
- True
- >>> _is_listlike(set())
- True
- >>> _is_listlike((x for x in range(3)))
- True
- >>> _is_listlike(range(5))
- True
- """
- return hasattr(x, "__iter__") and not isinstance(x, (str, bytes))
- if __name__ == '__main__':
- print(flatten([[[1, 2, 3, [7, 8]], (42, None)], [4, 5], [6], 7, (8, 9, 10)]))
复制代码
|
|