Create a compress function in Python?

后端 未结 19 1252
感动是毒
感动是毒 2021-01-05 03:37

I need to create a function called compress that compresses a string by replacing any repeated letters with a letter and number. My function should return the shortened vers

19条回答
  •  情深已故
    2021-01-05 04:22

    s=input("Enter the string:")
    temp={}
    result=" "
    for x in s:
        if x in temp:
            temp[x]=temp[x]+1
        else:
            temp[x]=1
    for key,value in temp.items():
        result+=str(key)+str(value)
    

    print(result)

提交回复
热议问题