0

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']

Jay Patel
  • 2,341
  • 2
  • 22
  • 43
  • set(mylist) ... oh you want counts? https://docs.python.org/3/library/collections.html#collections.Counter – Kenny Ostrom May 09 '19 at 13:59
  • 3
    `from collections import Counter` and then do `print(["%s(%d)"%(k,v) for k, v in Counter(mylist).items()])` – pault May 09 '19 at 14:01
  • What if you have something like `[1, 0, 1]`? Should the 1s be counted together? – gmds May 09 '19 at 14:02
  • I wanted like values to group together. The Counter suggestion was very helpful, I have been looking for an easy solution for a long time!\ – Jack Larson May 09 '19 at 14:05
  • Python works at a higher level of abstraction than some other languages (like C). If you find yourself writing a bunch of nested loops with a lot of if/else switches, you're probably not using a powerful built-in that you should be – Matt Messersmith May 09 '19 at 14:09

1 Answers1

3

You can use collections.Counter with a list comprehension:

a=[6,0,1,1,1,1,1,2,2,2,2,4,4,4,5,5]

from collections import Counter
print(["%s(%d)"%(k,v) for k, v in Counter(a).items()])
#['0(1)', '1(5)', '2(4)', '4(3)', '5(2)', '6(1)']

If you want to avoid printing the 1 in parentheses for the single items, you can do:

print(["%s(%d)"%(k,v) if v > 1 else str(k) for k, v in Counter(a).items()])
#['0', '1(5)', '2(4)', '4(3)', '5(2)', '6']
pault
  • 41,343
  • 15
  • 107
  • 149
  • You don't need square brackets in the print call, you can just pass a generator expression. Also you might want to use f-strings or .format for formatting – Boris Verkhovskiy May 09 '19 at 14:14