Oct-24-2020, 02:25 AM
(Oct-23-2020, 09:57 PM)bowlofred Wrote: Although it's at the front, it's still "inside the loop". This is just a syntax for a generator comprehension, similar to a list comprehension.
letter_generator = (i*char for i, char in enumerate(s, start=1))Is similar to:
def gen_expression(iterable): for i,char in enumerate(iterable, start=1): yield i*char letter_generator = gen_expression(s)And then the generator will hand out the sequence as requested.
Thanks. I came up with some similar which gets closer to my understanding of the function:
def tag(s): res = '' for i, char in enumerate(s, start=1): res += ''.join(i*char) return res print(tag('tom'))