Mailing List Archive

A missing iterator on itertools module?
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 ?
--
https://mail.python.org/mailman/listinfo/python-list
Re: A missing iterator on itertools module? [ In reply to ]
Le 28/03/2024 à 17:45, ast a écrit :

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

sorry
A Q W Z S X E D C


--
https://mail.python.org/mailman/listinfo/python-list
Re: A missing iterator on itertools module? [ In reply to ]
Le 28/03/2024 à 18:07, Stefan Ram a écrit :
> ast <none@none.fr> wrote or quoted:
>> 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")))
>
> 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 ?
--
https://mail.python.org/mailman/listinfo/python-list
Re: A missing iterator on itertools module? [ In reply to ]
Stefan Ram wrote:
> ast <none@none.fr> wrote or quoted:
>> 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.

`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.

> 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!

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.
--
https://mail.python.org/mailman/listinfo/python-list
Re: A missing iterator on itertools module? [ In reply to ]
Op 28/03/2024 om 17:45 schreef ast via Python-list:
> 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.

The documentation mentions a roundrobin recipe.
>
> 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']

But if your strings are not equal, this will only produce a partial result.
--
https://mail.python.org/mailman/listinfo/python-list
RE: A missing iterator on itertools module? [ In reply to ]
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=gmail.com@python.org> On
Behalf Of Antoon Pardon via Python-list
Sent: Wednesday, April 3, 2024 5:11 AM
To: python-list@python.org
Subject: Re: A missing iterator on itertools module?



Op 28/03/2024 om 17:45 schreef ast via Python-list:
> 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.

The documentation mentions a roundrobin recipe.
>
> 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']

But if your strings are not equal, this will only produce a partial result.
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list