Python: Minimum Numbers in List, Including Repeated Numbers

evictedSaint

kinda new to python, and I'm having trouble coming up with a solution to this problem.

Suppose I have a list A and suppose I want to return a list B containing at least the three smallest values, including repeated values. How would I write the logic for this? I'm at a bit of a loss as to how to translate this thought into code.

Example:

    A = [1,2,3,4,4]
    b = [1,2,3]

    A = [1,2,3,3,4]
    B = [1,2,3,3]

    A = [1,1,2,2,3]
    B = [1,1,2,2]

    A = [1,1,2,3]
    B = [1,1,2]
Paul Panzer

You can use the heapq module:

import heapq

def nsmallestwithrepeats(A, n=3):
    b = heapq.nsmallest(n, A)
    try:
        b = set(b)
    except TypeError:
        pass
    return [a for a in A if a in b]

for A in [1,2,3,4,4], [1,2,3,3,4], [1,1,2,2,3], [1,1,2,3]:
    print(nsmallestwithrepeats(A))

Output:

[1, 2, 3]
[1, 2, 3, 3]
[1, 1, 2, 2]
[1, 1, 2]

As @Mathieu Borderé points out instead of forming a set it is probably more efficient to simply compare to the largest element of b:

def nsmallestwithrepeats(A, n=3):
    b = heapq.nsmallest(n, A)
    return [a for a in A if a <= b[-1]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ordered list with repeated numbers

From Dev

Ordered list with repeated numbers

From Dev

Get the immediate minimum among a list of numbers in python

From Dev

Minimum of numbers that are not None in Python

From Dev

Find maximum length of consecutive repeated numbers in a list

From Dev

Convert a string into a list with integers including negative numbers

From Dev

Recursive method to find the minimum number in a list of numbers

From Dev

Minimum number of moves to equalize list of numbers

From Java

lambda if with list of numbers in python

From Dev

Normalizing a list of numbers in Python

From Dev

Python replacing numbers in a list

From Dev

Python add numbers in a list

From Dev

Create a list of repeated consecutive numbers regarding number from another list

From Dev

Parse list of numbers into list in python

From Dev

Creating NSArray of repeated numbers

From Dev

Repeated Random Numbers

From Dev

Remove repeated numbers in a sequence

From Dev

Return an array of the repeated numbers

From Dev

Range with repeated consecutive numbers

From Dev

MersenneTwister repeated numbers

From Java

Is there a way to coalesce repeated numbers in a list using streams in Java 8?

From Dev

Is there a way to coalesce repeated numbers in a list using streams in Java 8?

From Dev

Minimum of three numbers

From Dev

Minimum numbers of attacks needed

From Dev

Permutations of a list of input numbers in Python

From Dev

Python CSV multiple numbers in the list

From Dev

python convert bytearray to numbers in list

From Dev

creating list of random numbers in python

From Dev

Return positive numbers of a list in python

Related Related

HotTag

Archive