I am working on a project, and an annoying thing is that when I print a list e.g.('a','a','a','b','c','b')
, it will print:
a a a b c b
But, I would like it to join repeat values, e.g.:
a(3) b(2) c
I have a complicated function for doing this that still does not work(shown below) and does anyone have any suggestions?
def refine(testlist):
repeatfntest=0
prototypelist=testlist.copy()
lengthtest=len(testlist)-1
testlist.sort()
repititionfn=1
occurences=0
currentterm=prototypelist[lengthtest]
finalizedtermsfn=[]
while lengthtest>-1:
repititionfn=1
lengthtest=len(prototypelist)-1
occurences=0
while repititionfn>0:
lengthtest-=1
occurences+=1
print(currentterm)
prototypelist.remove(testlist[lengthtest])
if currentterm in prototypelist:
repititionfn=1
else:
repititionfn=0
if repititionfn==0 and occurences>1:
try:
finalizedtermsfn.append(str(currentterm)+"("+str(occurences)+")")
repititionfn=1
occurences=0
currentterm=prototypelist[lengthtest]
except:
print("Fail")
del finalizedtermsfn[-1]
elif repititionfn==0 and occurences==1:
try:
finalizedtermsfn.append(str(prototypelist[lengthtest]))
repititionfn=1
occurences=0
currentterm=prototypelist[lengthtest]
except:
print("Fail")
else:
currentterm=prototypelist[lengthtest]
return(finalizedtermsfn)
a=[6,0,1,1,1,1,1,2,2,2,2,4,4,4,5,5]
print(refine(a))
This Prints:
['5(2)','4(3)','2(4)','1(5)','6']