简体   繁体   English

如何从计数器中删除单个元素并将其作为列表返回?

[英]How do i remove single element from counter and return is as a list?

Given a list and a number, create a new list that contains each number of list at most N times, without reordering.给定一个列表和一个数字,创建一个新列表,该列表包含最多 N 次列表中的每个数字,而不重新排序。 For example if the input number is 2, and the input list is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].例如,如果输入数字为 2,输入列表为 [1,2,3,1,2,1,2,3],则取 [1,2,3,1,2],删除下一个 [ 1,2] 因为这会导致 1 和 2 在结果中出现 3 次,然后取 3,这会导致 [1,2,3,1,2,3]。 With list [20,37,20,21] and number 1, the result would be [20,37,21]使用列表 [20,37,20,21] 和数字 1,结果将是 [20,37,21]

For example i had this like my attempt例如我有这样的尝试

from collections import Counter

def delete_nth(order, max_e):
    c = Counter(order)
    result = []
    for key, value in c.items():
        if value > max_e:
            value = value - 1
    return [x for x in c.items()]

and output is输出是

dict_items([(20, 2), (37, 1), (21, 1)])
dict_items([(20, 2), (37, 1), (21, 1)])
From list [20, 37, 20, 21], 1 you get: [(20, 2), (37, 1), (21, 1)] should equal [20, 37, 21]
Log
dict_items([(1, 2), (3, 2), (7, 1), (2, 4)])
dict_items([(1, 2), (3, 2), (7, 1), (2, 4)])
From list [1, 1, 3, 3, 7, 2, 2, 2, 2], 3 you get: [(1, 2), (3, 2), (7, 1), (2, 4)] should equal [1, 1, 3, 3, 7, 2, 2, 2]
Log
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
dict_items([(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)])
From list [1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], 3 you get: [(1, 5), (2, 4), (3, 4), (4, 1), (5, 1)] should equal [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5]
From list [1, 1, 1, 1, 1], 5 you get: [(1, 5)] should equal [1, 1, 1, 1, 1]
Test Passed
Random tests
Testing for delete_nth([21, 45, 29, 29, 29, 6, 24, 45, 45, 45, 29, 21, 29, 24, 6, 45, 21, 38, 45, 21, 24, 45, 24, 6, 6, 29, 45, 29, 6, 45, 21, 29], 4)
Log
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
dict_items([(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)])
It should work on random inputs too!: [(21, 5), (45, 9), (29, 8), (6, 5), (24, 4), (38, 1)] should equal [21, 45, 29, 29, 29, 6, 24, 45, 45, 45, 29, 21, 24, 6, 21, 38, 21, 24, 24, 6, 6]
Testing for delete_nth([18, 18, 18, 18, 6, 23, 18, 46, 18, 31, 18, 31, 23, 18, 18, 31], 7)
Log
dict_items([(18, 9), (6, 1), (23, 2), (46, 1), (31, 3)])
dict_items([(18, 9), (6, 1), (23, 2), (46, 1), (31, 3)])
It should work on random inputs too!: [(18, 9), (6, 1), (23, 2), (46, 1), (31, 3)] should equal [18, 18, 18, 18, 6, 23, 18, 46, 18, 31, 18, 31, 23, 31]

Doing value = value - 1 only decrements the local variable value inside the loop;执行value = value - 1只会减少循环内的局部变量value it doesn't decrement the actual value inside the Counter.它不会减少计数器内的实际值。

I might try building the list inside the loop as you decrement the counter:当您递减计数器时,我可能会尝试在循环内构建列表:

from collections import Counter

def delete_nth(order, max_e):
    # Build counter with maximum count max_e.
    counts = {
        i: min(max_e, count)
        for i, count in Counter(order).items()
    }
    # Build result by iterating over original list.
    result = []
    for i in order:
        if not counts[i]:
            continue
        counts[i] -= 1
        result.append(i)
    return result

assert delete_nth([1,2,3,1,2,1,2,3], 2) ==  [1,2,3,1,2,3]

Perhaps something like this?也许是这样的?

def keep_at_most_n(arr, n):
    count = {}
    output = []
    for element in arr:
        if count.get(element, 0) < n:
            output.append(element)
            count[element] = count.get(element, 0) + 1
    return output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM