Python: return values from a loop without breaking out
问题 G'day, I have a list of individuals that are grouped by place. I want to produce a new variable that gives a number to each individual dependant on their place. What I would like my data to look like is: place individual here 1 here 2 here 3 there 1 there 2 somewhere 1 somewhere 2 I have written this: nest="ddd", "ddd", "fff", "fff", "fff", "fff", "qqq", "qqq" def individual(x): i = 0 j = 1 while i < len(x): if x[i] == x[i-1]: print(j+1) i = i + 1 j = j + 1 else: print(1) i = i + 1 j = 1