Posts: 8,160
Threads: 160
Joined: Sep 2016
It's nice solution using functools.singledispatch, but not supported in python2.
Posts: 2,953
Threads: 48
Joined: Sep 2016
(Aug-06-2018, 10:26 AM)perfringo Wrote: This is the reason I like Python - smart people (like buran) can achieve desired results with minimal and very readable piece of code. I have lot of work to do in smart part We all are smart people. I just don't know enough. I've got RecursionError and it was said to me that it's because the strings are iterable. Then I saw how can be done for strings. I see it now too. I still don't get it why if I use a generator in the way I've done it I get this error. Yes, strings are iterable but I tried it on a small piece of data.
Something happens internally and I just don't know what it is. If one can explain it, please!
Posts: 4,790
Threads: 76
Joined: Jan 2018
(Aug-06-2018, 10:44 AM)buran Wrote: It's nice solution using functools.singledispatch, but not supported in python2. Of course it is, you only need to pip install singledispatch , then
from singledispatch import singledispatch
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Aug-06-2018, 11:52 PM
(This post was last modified: Aug-06-2018, 11:52 PM by Skaperen.)
i like to have my code, especially functions, work in both Python2 (until at least Python2 EOL) and Python3 (back as far as possible) without using any modules. and if it really needs a module, preferably one that comes in Python itself, and if not that, one that can be installed with pip. i think my own flatten function managed to do that. it was a bit longer, but "simpler" in the sense of not using things like generators an subgenerators. my technique to manage the recursion was sloppy. buran did better and it still meets my goals (like not needing a module). even better is that it doesn't need variable arguments like i used. and even better is that the whole thing is functional.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,790
Threads: 76
Joined: Jan 2018
Aug-07-2018, 07:20 AM
(This post was last modified: Aug-07-2018, 07:20 AM by Gribouillis.)
The advantage of singledispatch is modularity. Custom types can be easily added with their own ways of being flattened.
Posts: 2,126
Threads: 11
Joined: May 2017
Aug-07-2018, 07:29 AM
(This post was last modified: Aug-07-2018, 07:30 AM by DeaD_EyE.)
I have don this a while ago: https://pastebin.com/1D0J3Sdn
def flat_sequnce_gen(input_list, traverse=None, exclude=None, debug=False):
traverse = traverse or (list, tuple)
exclude = exclude or (str, bytes, int, float, complex)
iterators = [iter(input_list)]
last = None
while iterators:
try:
current = next(iterators[-1])
except StopIteration:
iterators.pop()
continue
if isinstance(current, traverse) and not isinstance(current, exclude):
if last is current:
yield current
iterators.pop()
continue
iterators.append(iter(current))
last = current
else:
yield current There is also possibility for improvement.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
i went with buran's solution for my function collection.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 1,950
Threads: 8
Joined: Jun 2018
While reading Python Cookbook, 3rd Edition by David Beazley and Brian K. Jones I stumbled upon recipe 4.14. Flattening a Nested Sequence.
from collections import Iterable
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, ignore_types)
else:
yield x
items = [1, 2, [3, 4, [5, 6], 7], 8]
# Produces 1 2 3 4 5 6 7 8
for x in flatten(items):
print(x) Quote:In the code, the isinstance(x, Iterable) simply checks to see if an item is iterable. If so, yield from is used to emit all of its values as a kind of subroutine. The end result is a single sequence of output with no nesting.
The extra argument ignore_types and the check for not isinstance(x, ignore_types) is there to prevent strings and bytes from being interpreted as iterables and expanded as individual characters.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|