Discussion:
A missing iterator on itertools module?
(too old to reply)
ast
2024-03-28 16:45:42 UTC
Permalink
Hello

Suppose I have these 3 strings:

s1 = "AZERTY"
s2 = "QSDFGH"
s3 = "WXCVBN"

and I need an itertor who delivers

A Q W Z S C E D C ...

I didn't found anything in itertools to do the job.

So I came up with this solution:


list(chain.from_iterable(zip("AZERTY", "QSDFGH", "WXCVBN")))

['A', 'Q', 'W', 'Z', 'S', 'X', 'E', 'D', 'C', 'R', 'F', 'V', 'T', 'G',
'B', 'Y', 'H', 'N']

Do you havbe a neat solution ?
ast
2024-03-28 16:47:32 UTC
Permalink
Post by ast
A Q W Z S C E D C ...
sorry
A Q W Z S X E D C
Stefan Ram
2024-03-28 17:07:25 UTC
Permalink
Post by ast
s1 = "AZERTY"
s2 = "QSDFGH"
s3 = "WXCVBN"
and I need an itertor who delivers
A Q W Z S C E D C ...
I didn't found anything in itertools to do the job.
list(chain.from_iterable(zip("AZERTY", "QSDFGH", "WXCVBN")))
Maybe you meant "zip(s1,s2,s3)" as the definition of s1, s2,
and s3 otherwise would not be required. Also the "list" is not
necessary because "chain.from_iterable" already is an iterable.
You could also use "*" instead of "list" to print it. So,

import itertools as _itertools
s =[ "AZERTY", "QSDFGH", "WXCVBN" ]
print( *_itertools.chain.from_iterable( zip( *s )))

. But these are only minor nitpicks; you have found a nice solution!
ast
2024-03-28 17:12:54 UTC
Permalink
Post by Stefan Ram
Post by ast
s1 = "AZERTY"
s2 = "QSDFGH"
s3 = "WXCVBN"
and I need an itertor who delivers
A Q W Z S C E D C ...
I didn't found anything in itertools to do the job.
list(chain.from_iterable(zip("AZERTY", "QSDFGH", "WXCVBN")))
Maybe you meant "zip(s1,s2,s3)" as the definition of s1, s2,
and s3 otherwise would not be required. Also the "list" is not
necessary because "chain.from_iterable" already is an iterable.
You could also use "*" instead of "list" to print it. So,
import itertools as _itertools
s =[ "AZERTY", "QSDFGH", "WXCVBN" ]
print( *_itertools.chain.from_iterable( zip( *s )))
. But these are only minor nitpicks; you have found a nice solution!
Why did you renamed itertools as _itertools ?
Stefan Ram
2024-03-28 17:23:14 UTC
Permalink
Post by ast
Why did you renamed itertools as _itertools ?
Assume I have a module A.py:

import math
def f(): pass

. Assume I have an additional module B.py:

import A

. Now, when I'm editing "B.py" in IDLE and type "A.", IIRC
IDLE will offer me two possible completions: "A.math" and
"A.f". The "A.math" makes no sense to me. I want it to go
away. Therefore, I rewrite A.py as:

import math as _math
def f(): pass

. Now, Idle will only offer the completion "A.f".

So, I sometimes use this "import math as _math" style. But then,
it is simpler for me to /always/ use this style; after all: you
can't know whether someone eventually will import your module!
Lawrence D'Oliveiro
2024-03-28 22:18:09 UTC
Permalink
Post by Stefan Ram
. Now, when I'm editing "B.py" in IDLE and type "A.", IIRC
IDLE will offer me two possible completions: "A.math" and
"A.f". The "A.math" makes no sense to me.
It works, though.
Mark Bourne
2024-03-29 11:27:20 UTC
Permalink
Post by Stefan Ram
Post by ast
Why did you renamed itertools as _itertools ?
import math
def f(): pass
import A
. Now, when I'm editing "B.py" in IDLE and type "A.", IIRC
IDLE will offer me two possible completions: "A.math" and
"A.f". The "A.math" makes no sense to me.
`import math` imports the `math` module and binds it to `math` in the
global namespace of the `A` module. Since it doesn't have a leading
underscore, by default it's considered to be a public attribute of the
`A` module, and IDLE is offering all the public attributes of the `A`
module for completion.
Post by Stefan Ram
I want it to go
import math as _math
def f(): pass
. Now, Idle will only offer the completion "A.f".
So, I sometimes use this "import math as _math" style. But then,
it is simpler for me to /always/ use this style; after all: you
can't know whether someone eventually will import your module!
You can explicitly declare the public interface of a module by defining
`__all__`, listing the names which should be considered part of the
module's public interface; see:
- https://docs.python.org/3/reference/simple_stmts.html#the-import-statement
- https://peps.python.org/pep-0008/#public-and-internal-interfaces

Although `from A import *` is generally discouraged, if `A` defines
`__all__` then only the names listed in `__all__` are bound in the
importing module's namespace. Otherwise, all names from `A` which don't
have a leading underscore are considered to be public and bound in the
importing module.

I don't use IDLE, but it may be that it also uses `__all__` to determine
a module's public API. In that case, setting `__all__ = ["f"]` in `A`
should prevent it from offering `math` as a completion (nor any other
name that's not in the `__all__` list).
--
Mark.
Antoon Pardon
2024-04-03 09:11:01 UTC
Permalink
Post by ast
Hello
s1 = "AZERTY"
s2 = "QSDFGH"
s3 = "WXCVBN"
and I need an itertor who delivers
A Q W Z S C E D C ...
I didn't found anything in itertools to do the job.
The documentation mentions a roundrobin recipe.
Post by ast
list(chain.from_iterable(zip("AZERTY", "QSDFGH", "WXCVBN")))
['A', 'Q', 'W', 'Z', 'S', 'X', 'E', 'D', 'C', 'R', 'F', 'V', 'T', 'G',
'B', 'Y', 'H', 'N']
But if your strings are not equal, this will only produce a partial result.
a***@gmail.com
2024-04-03 12:14:53 UTC
Permalink
Antoon,

Even if the suggested solution offers a partial result, you would need
specific requirements to determine what should be done if one or more of the
parts being cycled is shorter than the others. Stopping at that point is one
option. Another is to continue but only interleave ones still producing and
in the same order.

There is a function in itertools called zip_longest() that might be
considered as it keeps going but substitutes a customizable value for
"missing" parts. You could then, perhaps, make a change so that sentinel is
not passed along.


-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=***@python.org> On
Behalf Of Antoon Pardon via Python-list
Sent: Wednesday, April 3, 2024 5:11 AM
To: python-***@python.org
Subject: Re: A missing iterator on itertools module?
Post by ast
Hello
s1 = "AZERTY"
s2 = "QSDFGH"
s3 = "WXCVBN"
and I need an itertor who delivers
A Q W Z S C E D C ...
I didn't found anything in itertools to do the job.
The documentation mentions a roundrobin recipe.
Post by ast
list(chain.from_iterable(zip("AZERTY", "QSDFGH", "WXCVBN")))
['A', 'Q', 'W', 'Z', 'S', 'X', 'E', 'D', 'C', 'R', 'F', 'V', 'T', 'G',
'B', 'Y', 'H', 'N']
But if your strings are not equal, this will only produce a partial result.
--
https://mail.python.org/mailman/listinfo/python-list
Loading...