Python and lists:flatten. Such as in Erlang
Python does not has function to transform nested lists in a flatten lists, such as lists:flatten in Erlang. This is my example.
Erlang:
1> L = [1,[2,[4,4,4,[5,5,5,5]],2,2,2,[3,3,3,3]],3,[4,5,6],[7,[8,[9,10]]]].
2> lists:flatten(L).
[1, 2, 4, 4, 4, 5, 5, 5, 5, 2, 2, 2, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10]
3>
Python:
def flat_list(ls,new_ls=[]):
for i in ls:
if type(i) == list:
flat_list(i,new_ls)
else:
new_ls.append(i)
return new_ls
>>> ls = [1, [2, [4, 4, 4, [5, 5, 5, 5]], 2, 2, 2, [3, 3, 3, 3]], 3, [4, 5, 6], [7, [8, [9, 10]]]]
>>> flat_list(ls)
[1, 2, 4, 4, 4, 5, 5, 5, 5, 2, 2, 2, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10]
Читайте також:
- Python + CouchDB
- flash socket policy-file-request in Erlang
- Erlang: Hello World
- Mustache is a framework templating system
- kerl - easy building and installing of Erlang/OTP instances
- md5 в Erlang
- URL encode in Erlang
- Erlang: cpu usage
- Webmachine redirect resource example
- Erlang uuid