• Re: A missing iterator on itertools module?

    From ast@21:1/5 to All on Thu Mar 28 18:12:54 2024
    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 ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to ast on Thu Mar 28 17:07:25 2024
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ast@21:1/5 to All on Thu Mar 28 17:47:32 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ast@21:1/5 to All on Thu Mar 28 17:45:42 2024
    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 ?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to ast on Thu Mar 28 17:23:14 2024
    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. 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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Stefan Ram on Thu Mar 28 22:18:09 2024
    On 28 Mar 2024 17:23:14 GMT, Stefan Ram wrote:

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mark Bourne@21:1/5 to Stefan Ram on Fri Mar 29 11:27:20 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Antoon Pardon@21:1/5 to All on Wed Apr 3 11:11:01 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)