On Tue, 7 Jun 2022 at 10:26, Steven D'Aprano wrote:
>
> Why don't you use the version from the itertools recipes?
>
>
> ```
> from itertools import tee, filterfalse
> def partition(pred, iterable):
> "Use a predicate to partition entries into false entries and true entries"
> # partition(i
Why don't you use the version from the itertools recipes?
```
from itertools import tee, filterfalse
def partition(pred, iterable):
"Use a predicate to partition entries into false entries and true entries"
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
t1, t2 = tee(ite
On Mon, Jun 06, 2022 at 06:17:32PM +0200, Benedict Verhegghe wrote:
> 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, 1
This idea reminds me of numpy fancy indexing: that is, you can pass
multiple indexes in at once. (See bumpy docs for details). I have no idea
how that might translate to ducts (tulles are perfectly reasonable keys
already) but I know I like it in numpy :-)
That being said, I can’t remember a use c
On Tue, 7 Jun 2022 at 01:58, David Mertz, Ph.D. wrote:
> In [2]: def isEven(n):
>...: return n/2 == n//2
>...:
>
Huh. That's a very strange way to write that predicate.
>>> isEven(2)
True
>>> isEven(20001)
True
>>> isEven(20002)
False
>>> isEve
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:
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
This is really nice, but I think you have a negation wrong in there
somewhere:
In [1]: from collections import deque
...:
...: def partition(pred, iterable):
...: results = deque([]), deque([])
...:
...: def gen_split(only):
...: for thing in iterable:
...:
Thanks for all the comments.
I did consider using sets but order was important and when partitioning a list
of general strings, not file names, there could be identical and important
strings (strings at different positions could mean different things, especially
when generating test and train
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[not pred(thing)].append(thing)
for thing
As Chris Angelico notes, that's only good for finite iterables.
The recipe in the itertools docs is unbounded, but it's also unbounded in
the cache needed if you get long strings of things the do (or don't)
confirm the predicate.
On Mon, Jun 6, 2022 at 7:06 AM Stephen J. Turnbull <
stephenjturnb.
Christopher Barker writes:
> How do you divide a sequence into two sequences cleanly?
I don't know how to do it in a one liner, but
yes = []
no = []
for elem in seq:
(yes if test(elem) else no).append(elem)
seems pretty clean to me.
On Mon, 6 Jun 2022 at 18:22, Paul Moore wrote:
>
> There’s an itertools recipe, “partition”.
>
Yes, there is (and plenty of equivalents in other languages). But it's
still not what I'd call "particularly easy". What that recipe does is
filter the same iterable with the same predicate twice, so it
On Mon, 6 Jun 2022 at 18:02, Stephen J. Turnbull
wrote:
>
> David Mertz, Ph.D. writes:
>
> > These are all far too easy to do with comprehensions to merit new methods
> > or stdlib functions.
>
> +1
>
> > E.g., we might provide additional set-like operators for dicts.
>
> > >>> m - {'a'} # Wo
06.06.22 10:51, Stephen J. Turnbull пише:
Couldn't it be linear on min(len(left), len(right))? Ie,
if len(left) < len(right):
for elem in left:
if elem in right:
left.discard(elem)
else:
for elem in right:
left.discard(elem)
There’s an itertools recipe, “partition”.
On Mon, 6 Jun 2022 at 08:28, Chris Angelico wrote:
> On Mon, 6 Jun 2022 at 16:42, Christopher Barker
> wrote:
> >
> > I think y’all have addressed the OP’s problem — at least the performance
> issues.
> >
> > But I think this brings up a pattern that I
David Mertz, Ph.D. writes:
> These are all far too easy to do with comprehensions to merit new methods
> or stdlib functions.
+1
> E.g., we might provide additional set-like operators for dicts.
> >>> m - {'a'} # Would rightval be a set or dict though?!
> >>> m & {'a', 'b'} # Same questi
David Mertz, Ph.D. writes:
> This is exactly the problem solved by set difference. E.g.
> `{a, b, c} - {a, c}`.
>
> This operation is linear on the size of the removed set.
Couldn't it be linear on min(len(left), len(right))? Ie,
if len(left) < len(right):
for elem in left:
On Mon, 6 Jun 2022 at 16:42, Christopher Barker wrote:
>
> I think y’all have addressed the OP’s problem — at least the performance
> issues.
>
> But I think this brings up a pattern that I don’t have a nifty way to address:
>
> How do you divide a sequence into two sequences cleanly?
>
> The fil
19 matches
Mail list logo