牛客网-寻找Coder

python2.7

# -*- coding:utf-8 -*-

class Coder:
    def findCoder(self, A, n):
        # write code here
        times=[]
        for i in A:
            if i.count('coder')!=0:
                times.append([i.count('coder'),i])
        times.sort(key=lambda x:x[0],reverse=True)  #降序
        return [i[1] for i in times]

【总结】:
使用append给list赋值的时候,可以赋多个值,用[]组合起来即可。
计数: .count()
排序: .sort(key,reverse=True)表示降序,reverse=False表示升序

猜你喜欢

转载自blog.csdn.net/aaon22357/article/details/83572931