简体   繁体   English

使用for循环检查列表时如何追加到新列表项?

[英]How to append to new list item when checking list with for loop?

I am having problems with this and can't figure out why my code is not working. 我对此有疑问,无法弄清楚为什么我的代码无法正常工作。 Could someone help me out with this, please? 有人可以帮我这个忙吗? I made this: 我做的:

def f8(a_list, n):
    """
    The parameter a_list is a list of int's. The parameter n is an int.
    The function f8() should return a list that contains exactly one
    of each number in a_list that occurs less than n times in a_list.

    Example:
        f8([1, 1, 7, 7, 7, 3, 3, 3, 4, 4, 4, 5, 5], 3) should return
        [1, 5] (as 1 and 5 are the only numbers that occur less than 3
        times in a_list)
    """

    k = []
    for i in a_list:
        if i < a_list.count(n):
            k.append(i)
            return k


print f8([1, 7, 7, 3, 3, 3, 4, 4, 5], 2)

I would expect it should print [1,5], but it just gives me a None. 我希望它应该打印[1,5],但它只是给我一个None。 Why is that? 这是为什么? Could someone help me out, please? 有人可以帮我吗? I am stuck here. 我被困在这里。

You have the counting the wrong way around! counting错了方向! You need to count the occurrences of i and compare this against n . 您需要计算i的出现并将其与n进行比较。 Also, you need to move the return outside the for-loop . 另外,您需要将return移到for-loop Finally, to remove the repeats in the final list , we should iterate through set(a_list) so that we only iterate through unique elements . 最后,要删除最终list中的重复项,我们应该遍历set(a_list)以便仅遍历unique elements The neat thing about this is that since we are counting the occurrences in the original a_list , we don't need to create any copies or anything fiddly to deal with this. 这样做的a_list是,由于我们要在原始a_list中计数出现的次数,因此无需创建任何副本或任何笨拙的事情来处理此问题。

This makes your function : 这使您的function

def f8(a_list, n):
    k = []
    for i in set(a_list):
        if a_list.count(i) < n:
            k.append(i)
    return k

which works if we give it a test: 如果我们给它一个测试,它会起作用:

>>> f8([1, 1, 7, 7, 7, 3, 3, 3, 4, 4, 4, 5, 5], 3)
[1, 5]
>>> f8([1, 1, 2, 2, 2, 2, 4, 5, 6, 7, 7, 7, 7], 3)
[1, 4, 5, 6]

Note, if you wanted to shorten the function , you could achieve the same result in a one line list-comprehension : 注意,如果您想缩短该function ,则可以在one line list-comprehension获得相同的结果:

def f8(a_list, n):
    return [i for i in set(a_list) if a_list.count(i) < n]

which gives the same outputs to the tests as above. 与上述测试输出相同。

Firstly, you should not call count repeatedly. 首先,您不应重复调用count Each call iterates the entire list. 每个调用都会迭代整个列表。 You can get all counts in one go using collections.Counter . 您可以使用collections.Counter一次性获得所有计数。 Secondly, you need to check if you have added any element before to avoid duplicates, just calling set one a_list will not guarantee order of appearance: 其次,您需要检查是否已添加任何元素,以避免重复,仅调用set one a_list将不能保证出现的顺序:

from collections import Counter

def f8(a_list, n):
    c = Counter(a_list)
    k, seen = [], set()
    for x in a_list:
        if c[x] < n and x not in seen:
            seen.add(x)
            k.append(x)
    return k

The condition to append in your code is never met, thus, no value is being returned. 永远不会满足append在代码中的条件,因此不会返回任何值。 Instead, return after the loop is finished. 而是在循环完成后返回。 Also, your counting condition is reversed, as you should be finding the count of i occurring in the list, not n : 另外,您的计数条件是相反的,因为您应该在列表中找到i的计数,而不是n

k = []
for i in a_list:
    if a_list.count(i) < n and i not in k:
        k.append(i)

return k

The reason why you are receiving None is that the return statement does not pass, so to satisfy the variable storage during the function call, None is returned. 收到None的原因是return语句没有通过,因此为了满足函数调用期间的变量存储,返回None

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

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