Op 6/06/2022 om 18:07 schreef Mathew Elman:
oops, you're right, I have ended up with an unnecessary "not" in there, should 
be:

from collections import deque


def partition(pred, iterable):
     results = deque([]), deque([])

     def gen_split(only):
         for thing in iterable:
             if results[only]:
                 yield results[only].popleft()
             results[pred(thing)].append(thing)
         for thing in results[only]:
             yield thing

     return gen_split(True), gen_split(False)

There still is something wrong. I get the second list twice:

odd, even = partition(lambda i: i % 2, range(20))
print(list(odd))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print(list(even))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JYGSSXPN6FLGHVJPUM4XTPH2EABZZMLS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to