一个简单的排序题目

实现

输入:

string='ac2at1fa3fgd2'

输出:

atacacfgdfgdfafafa

也就是string中字母后面的数字是该字母重复次数,ac 2,表示ac重复两次,排序依据是哪个数字小先排哪个字母串

from collections import Counter
string='ac2at1fa3fgd2'
num=[]
ap=[]
index=0
copy=string
di_count=0
ah_count=0
for  c in list(string):
    if c.isdigit():
        if (index-ah_count)==index:
            ap=ap
        else:
            ap.append(copy[index-ah_count:index])
        ah_count=0
        di_count+=1
        
    else:
        if (index-di_count)==index:
            ap=ap
        else:
            num.append(copy[index-di_count:index])
        di_count=0
        ah_count+=1
    index+=1
num.append(copy[index-di_count:index])
num_s=[]
for x in num:
    t=int(x)
    num_s.append(t)
C=Counter(num_s)
p=list(zip(ap,num_s))

s=sorted(p,key=lambda n:n[1])

tt=[]
for aph,num in s:
    for i in range(num):
        tt.append(aph)
print(''.join(tt))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值