python去重复排序_python实现:重复列表按重复次数排序

#重复列表按重复次数排序方法1

s = 'aacbddbcdadb'

lists1=list(s)

uniques1 = set(lists1)

dict_str = {}

for unique1 in uniques1:

i=0

for list1 in lists1:

if list1 == unique1:

i += 1

dict_str[unique1]=i

print(dict_str)

list2=[]

while dict_str:

max_count = max(dict_str.values())

for key,value in dict_str.items():

if value == max_count:

for i in range(0,value):

list2.append(key)

break

del dict_str[key]

print("after del dict_str-"+str(dict_str))

print(list2)

#重复列表按重复次数排序方法2

s = 'aacbddbcdadb'

lists = list(s)

uniquelist = set(lists)

dict={}

result_list = []

for unique in uniquelist:

dict[unique] = lists.count(unique)

print(dict)

while dict:

max_count = max(dict.values())

for key,value in dict.items():

if value == max_count:

for i in range(1,value+1):

result_list.append(key)

break

del dict[key]

print(result_list)

#重复列表按重复次数排序方法3 ------------这个方案比较好,对字典按照value进行了倒序排列,使用到了lambda匿名函数。

s = 'aacbddbcdadb'

lists = list(s)

uniquelist = set(lists)

dict={}

ordered_dict={}

result_list = []

for unique in uniquelist:

dict[unique] = lists.count(unique)

print(dict)

ordered_dict = sorted(dict.items(),key=lambda x:x[1],reverse=True) ##对字典按照value进行倒序排序,使用了匿名函数lambda

print(ordered_dict)

for key,value in ordered_dict:

for i in range(1,value+1):

result_list.append(key)

print(result_list)

# ordered_dict = sorted(dict.items(),key=lambda x:x[1],reverse=True)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值