I am stuck trying to perform this task and while trying I can’t help thinking there will be a nicer way to code it than the way I have been trying.
I have a line of text and a keyword. I want to make a new list going down each character in each list. The keyword will just repeat itself until the end of the list. If there are any non-alpha characters the keyword letter will not be used.
For example:
Keyword="lemon"
Text="hi there!"
would result in
('lh', 'ei', ' ', 'mt' , 'oh', 'ne', 'lr', 'ee', '!')
Is there a way of telling python to keep repeating over a string in a loop, ie keep repeating over the letters of lemon?
I am new to coding so sorry if this isn’t explained well or seems strange!
Here’s a solution:
Output
itertools.cycle iterates over a sequence repeatedly (a string is a sequence of characters). next gets the next character from the repeating sequence. The generator expression selects the pair of next keyword letter and text character if the text character is alphabetic, else it just selects the non-alphabetic character alone.