Compressing python strings

Asked 1 years ago, Updated 1 years ago, 116 views

String input is If aabbbccccddaa

Not a2b3c4d2a2 I want to print it out as a4b3c4d2

How should I make the code?

string count

2022-09-21 11:37

2 Answers

The problem of counting the number of elements and listing them as strings.

Using the Python built-in library, you can solve it as follows:

from collections import Counter
from itertools import chain


def compress(value):
    counter = Counter(value)
    return "".join(str(v) for v in chain.from_iterable(counter.items()))


assert compress("aabbbccccddaa") == "a4b3c4d2"


2022-09-21 11:37

 a = list(input())

 def k(c):

     b = 0
     for i in range(len(a)-1):
         b += a.count(a[i])
         For j in range(len(a), 0, -1):#Length????
             if a.count(a[i]) != 1:
                 del a[i]
         return b == 1
         print(a[i]+b,end="")
 print(k(a))

I've been trying to fix it, and now I'm stopping here and whining. What should I fix? ㅠ<


2022-09-21 11:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.