python自定义规律绘制_Python3 sorted() 自定义排序规则

面试题目:给定两个数N,K和N个字符串,输出2*K行,每行为字符串和该字符串出现的次数,对于前K行,按字符串出现次数降序排列,如果出现次数相同,则按字符串的字典序升序输出,后K行,按字符串出现次数升序排列,如果出现次数相同,则按字符串的字典序升序输出。

题解,其实就是对字符串S的一个计数+排序,排序的对象为:(出现次数,字符串),当使用出现次数Count无法判定大小的时候,使用额外的字符串本身的序列作为比较依据。

使用Java还是比较好写的,直接传给list.sort()一个比较器(Comparator)就行了:

class Row {

String string;

Integer count;

// getter and setter

@Override

public String toString() {

return string + " " + count;

}

}

......

list.sort((Row o1, Row o2) -> {

if (o1.getCount() > o2.getCount()) {

return -1;

} else if (o1.getCount().equals(o2.getCount())) {

return o1.getString().compareTo(o2.getString());

} else {

return 1;

}

});

list.stream().limit(k).forEach(System.out::println);

......

但是之前没有太使用过Python里面的这个比较器,之前的有限经验里面使用的比较多的就是字典的排序了:

# 按照键排

t = sorted(d.items(), key=lambda d: d[0])

# 按照值排

t = sorted(d.items(), key=lambda d: d[1])

没有使用到两个以上的排序属性的排序,查阅之后,发现还是同样使用sorted(),有如下代码:

import random

from functools import cmp_to_key

# n = int(input())

# a = list(map(int, input().split()))

n = 10

a = [random.randint(0, n) for i in range(n)]

def comparator01(o1, o2):

if o1[1] > o2[1]:

return -1

elif o1[1] == o2[1]:

if o1[0] > o2[0]:

return 1

elif o1[0] == o2[0]:

return 0

else:

return -1

else:

return 1

def comparator02(o1, o2):

if o1[1] > o2[1]:

return 1

elif o1[1] == o2[1]:

if o1[0] > o2[0]:

return 1

elif o1[0] == o2[0]:

return 0

else:

return -1

else:

return -1

d = {}

for i in a:

if i in d.keys():

d[i] += 1

else:

d[i] = 1

t = sorted(d.items(), key=cmp_to_key(comparator01))

limit = n // 3

for i in range(limit):

print('{} {}'.format(*t[i]))

t = sorted(d.items(), key=cmp_to_key(comparator02))

for i in range(limit):

print('{} {}'.format(*t[i]))

需要自定义比较器的逻辑,之后使用functools里面的cmp_to_key()将比较逻辑进行包装,传递给key参数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值