Kaydet (Commit) 385c8036 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Merge

...@@ -185,6 +185,8 @@ def nlargest(n, iterable): ...@@ -185,6 +185,8 @@ def nlargest(n, iterable):
Equivalent to: sorted(iterable, reverse=True)[:n] Equivalent to: sorted(iterable, reverse=True)[:n]
""" """
if n < 0:
return []
it = iter(iterable) it = iter(iterable)
result = list(islice(it, n)) result = list(islice(it, n))
if not result: if not result:
...@@ -201,6 +203,8 @@ def nsmallest(n, iterable): ...@@ -201,6 +203,8 @@ def nsmallest(n, iterable):
Equivalent to: sorted(iterable)[:n] Equivalent to: sorted(iterable)[:n]
""" """
if n < 0:
return []
if hasattr(iterable, '__len__') and n * 10 <= len(iterable): if hasattr(iterable, '__len__') and n * 10 <= len(iterable):
# For smaller values of n, the bisect method is faster than a minheap. # For smaller values of n, the bisect method is faster than a minheap.
# It is also memory efficient, consuming only n elements of space. # It is also memory efficient, consuming only n elements of space.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment