• A missing iterator on itertools module?

    From ast@3:633/280.2 to All on Fri Mar 29 03: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 ?

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Guest of ProXad - France (3:633/280.2@fidonet)
  • From ast@3:633/280.2 to All on Fri Mar 29 03: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



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Guest of ProXad - France (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Fri Mar 29 04: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!

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From ast@3:633/280.2 to All on Fri Mar 29 04: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 ?

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Guest of ProXad - France (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Fri Mar 29 04: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!

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Mark Bourne@3:633/280.2 to All on Fri Mar 29 22: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.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Antoon Pardon@3:633/280.2 to All on Wed Apr 3 20: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.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From avi.e.gross@gmail.com@3:633/280.2 to All on Wed Apr 3 23:14:53 2024
    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


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)