I am new to python and asked a question about code vectorization
def makeNames2(nList):
for nLi in nList:
nLIdx=[i for i,j in enumerate(nList) if j==nLi]
if nLIdx.__len__()>1:
for i,j in enumerate(nLIdx):
if i>0: nList[j]=nList[j]+str(i)
return nList
which performs the following actions:
>>> nLTest=['asda','asda','test','ada','test','yuil','test']
>>> print(makenames2(nLTest)
['asda', 'asda1', 'test', 'ada', 'test1', 'yuil', 'test2']
The code works fine, but I was wondering if there is a way to vectorize the loops for?
EDIT
Thanks to all for all three answers. This is exactly what interests me and I would like to select all the answers. I can not choose more than one, but they all work.
source
share