Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiplying an iterator
#1
if this didn't raise an exception, would you expect it to produce an iterator that multiplies this given iterator?
given = reversed('foobar')
other = 6 * given
final = ''.join(x for x in other)
print(final)
this would be expected to work if a str were assigned in line 1 (making line 3 not needed). how would you fix something like this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
given = reversed("foobar")
other = 6 * tuple(given)
final = "".join(other)
print(final)
Output:
raboofraboofraboofraboofraboofraboof
or
print(6 * "".join(reversed("foobar")))
Output:
raboofraboofraboofraboofraboofraboof
or
print(6 * "foobar"[::-1])
Output:
raboofraboofraboofraboofraboofraboof
Reply
#3
but you multiplied a tuple, not an iterator. what i'm suggesting is than if Python had the ability to multiply an iterator, it should make sense for the product to be a new iterator that carries out iterating the multiplicand iterator (maybe a copy of it) that many times. likewise, adding iterators should give a sum iterator that carries out iterating over all the added iterators. subtraction and division would not make sense in most cases. i can imagine comparing iterators and applying len().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
itertools.repeat? There are likely other functions in itertools that are useful to you and there are a whole bunch more in the Toolz library.
Reply
#5
i'm interested in studying the ways using iterators could be expanded in case some these make good use cases and make sense for how they get used. i think adding any number of iterators might make sense as well as multiplying them by an int. i might try to make a generator that does this.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Maybe I misunderstood what you meant by "multiplying them by an int". In any case, did you look at the libraries I mentioned? There's a lot of useful stuff in both.
Reply
#7
i have looked at itertools. i've used a few several times even before starting this thread. anything in particular you think i should look at?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Just knowing that it exists really and checking there first to see if there's something that solves a problem for you, before you reinvent the wheel. The same goes for Toolz.
Reply
#9
you are giving me two module names to check. that suggests to me that you don't really know where it is. if you don't that further suggests you don't really know if it does exist.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
i cannot find Toolz. itertools.repeat might be the closest in concept but it just gives a repetition of iterators, not one iterator that has all the repetition inside.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  testing if an iterator is empty Skaperen 9 8,989 Feb-25-2022, 02:31 AM
Last Post: Skaperen
  iterating an iterator by one step Skaperen 0 1,581 Feb-09-2021, 08:38 PM
Last Post: Skaperen
  modifying the origin of an iterator Skaperen 4 2,941 Mar-05-2020, 11:49 PM
Last Post: Skaperen
  multiplying integer to decimal ArnabRoyBatman 8 12,581 Jun-20-2017, 04:49 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020