How do I get the colour codes to restart once it comes to an end?
itertools.cycle
to the rescue:
import itertools
r = [135,147,196,211]
g = [193,208,229,117]
b = [196,35,135,3]
listone = ["Apple","Toyota","Civic","Merc","Toshiba","Samsung","Dell","Turbo"]
colours = zip(r,g,b)
colours_and_names = zip(listone,itertools.cycle(colours))
#=> [('Apple', (135, 193, 196)), ('Toyota', (147, 208, 35)), ('Civic', (196, 229, 135)), ('Merc', (211, 117, 3)), ('Toshiba', (135, 193, 196)), ('Samsung', (147, 208, 35)), ('Dell', (196, 229, 135)), ('Turbo', (211, 117, 3))]
Now, let's fix up your code a little:
for (i, (word,colour)) in enumerate(colours_and_names):
Shape.TextFrame.TextRange.Characters(res[i], charlen[i]).Font.Color = RGB(*colour)
You can see that with sequence assignment, you can enumerate over a zipped structure, and capture the elements into variables; and the *
("splat") operator allows you to unpack a sequence into function arguments.
Now, if you want every word in every colour, you can use product:
list(itertools.product(listone,colours))
#=> [('Apple', (135, 193, 196)), ('Apple', (147, 208, 35)), ('Apple', (196, 229, 135)), ('Apple', (211, 117, 3)), ('Toyota', (135, 193, 196)), ('Toyota', (147, 208, 35)), ('Toyota', (196, 229, 135)), ('Toyota', (211, 117, 3)), ('Civic', (135, 193, 196)), ('Civic', (147, 208, 35)), ('Civic', (196, 229, 135)), ('Civic', (211, 117, 3)), ('Merc', (135, 193, 196)), ('Merc', (147, 208, 35)), ('Merc', (196, 229, 135)), ('Merc', (211, 117, 3)), ('Toshiba', (135, 193, 196)), ('Toshiba', (147, 208, 35)), ('Toshiba', (196, 229, 135)), ('Toshiba', (211, 117, 3)), ('Samsung', (135, 193, 196)), ('Samsung', (147, 208, 35)), ('Samsung', (196, 229, 135)), ('Samsung', (211, 117, 3)), ('Dell', (135, 193, 196)), ('Dell', (147, 208, 35)), ('Dell', (196, 229, 135)), ('Dell', (211, 117, 3)), ('Turbo', (135, 193, 196)), ('Turbo', (147, 208, 35)), ('Turbo', (196, 229, 135)), ('Turbo', (211, 117, 3))]