colours=[red,green,blue,yellow,red,red,black,blue,yellow] i want update list result should like:
colours=[red1,green,blue1,yellow1,red2,red3,black,blue2,yellow2] thanks
use counter identify repeated colours, , build dict of colour name itertools.count(1) object. use list-comp append next number colour colour has been repeated, eg:
from collections import counter itertools import count colours=['red','green','blue','yellow','red','red','black','blue','yellow'] repeated = {k:count(1) k, v in counter(colours).items() if v > 1} new_colours = [colour + str(next(repeated[colour])) if colour in repeated else colour colour in colours] # ['red1', 'green', 'blue1', 'yellow1', 'red2', 'red3', 'black', 'blue2', 'yellow2']
Comments
Post a Comment