-
Notifications
You must be signed in to change notification settings - Fork 1
/
flatten.py
55 lines (46 loc) · 1.16 KB
/
flatten.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! usr/bin/env python3
"""
Flatten a list
from http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html
Flat2 from Python Cookbook, recipe 4.14
"""
from collections import Iterable
def flatten(l, ltypes=(list, tuple, set)):
ltype = type(l)
l = list(l)
i = 0
while i < len(l):
while isinstance(l[i], ltypes):
if not l[i]:
l.pop(i)
i -= 1
break
else:
l[i:i + 1] = l[i]
i += 1
return ltype(l)
def flat2(items, ignore_types=(str, bytes)):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, ignore_types):
yield from flat2(x, ignore_types)
else:
yield x
a = []
for i in range(20):
a = [a, i]
a = flatten(a)
# ___________my version
# def flatten(mylist):
# flatlist = []
# for x in mylist:
# if type(x) == tuple or type(x) == list or type(x) == set:
# for y in x:
# flatlist.append(y)
# else:
# flatlist.append(x)
#
# if type(x) == list:
# x = flatten(x)
# continue
#
# return flatlist