Skip to content

Instantly share code, notes, and snippets.

@sincerefly
Created April 30, 2015 02:12
Show Gist options
  • Save sincerefly/720dcede336117e56194 to your computer and use it in GitHub Desktop.
Save sincerefly/720dcede336117e56194 to your computer and use it in GitHub Desktop.
编写一个函数,接受两个参数,返回列表中最大的n个数字以列表形式返回
# 这次终于算是比较满意的符合pythonic的解决方式了
def largest(n, xs):
return sorted(xs)[len(xs)-n:]
print largest(2, [10,9,8,7,6,5,4,3,2,1])
# 不过看到一个更简洁的写法,好简洁,python真的是魅力无穷
def largest(n, xs):
"Find the n highest elements in a list"
return sorted(xs)[-n:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment