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]
Читайте також:
- Erlang: convert any string to binary utf8
- Erlang CouchDB fault-tolerance
- Random shuffle in erlang
- Erlang: Hello World
- flash socket policy-file-request in Erlang
- md5 в Erlang
- kerl - easy building and installing of Erlang/OTP instances
- Python + CouchDB
- URL encode in Erlang
- Django mongoengine cache backend