计算第二个序列中第一个序列的种子值

Counting seed values from first sequence in second sequence

如果给一个函数一个序列 seeds 的种子值,它需要计算每个种子值在第二个序列 xs 中出现的次数。然后它应该 return 计数作为整数列表,与种子值的顺序相同。如果 seeds 包含重复值,请将重复计数保留在 returned 列表中。例如,count_each([10,20],[10,20,50,20,40,20]) 应该 return [1,3]count_each('aeiou','encyclopedia') should return [1,2,1,1,0].

我对如何编写此功能感到困惑。这是我到目前为止所拥有的,但我无法弄清楚我将如何准确地计算第二个序列中种子值的数量,而不仅仅是检查这些值是否存在。对此的任何帮助将不胜感激。

def count_each(seeds,xs):   

    if seeds in xs:
        if seeds == True
    return seeds
        elif seeds == False
    return None

I can't figure out how exactly I would count the number of seed values in the second sequence and not just check if the values exist.

Python 序列有一个方便的 .count 方法。

>>> [1,1,1,2,3,4,5].count(1)
3
>>> "ababababcdefg".count("b")
4

查看collections.Counter()

import collections

def count_each(seeds,xs): 
    c = collections.Counter(xs)
    return [c[seed] for seed in seeds]
def count_letter(seeds,xs):
    for c in seeds:
        count=0
        for d in xs:
            if c==d:
                count=count+1
        print (c,"Occured ",count,"times")

count_letter([10,20],[10,20,30,10])
count_letter('aeiou','encyclopedia')