0

If a function is given a sequence seeds of seed values, it needs to count for every single seed value the number of times they show up in the 2nd sequence xs. Then it should return the counts as a list of integers, in the same order of the seed values. If seeds includes duplicated values, keep duplicated counts in the returned list. For example, count_each([10,20],[10,20,50,20,40,20]) should return [1,3] and count_each('aeiou','encyclopedia') should return [1,2,1,1,0].

I'm stuck on how to program this function. This is what I have so far but 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. Any help on this would be appreciated.

def count_each(seeds,xs):   

    if seeds in xs:
        if seeds == True
    return seeds
        elif seeds == False
    return None
Julien
  • 13,986
  • 5
  • 29
  • 53
famguy74
  • 27
  • 3
  • 11
  • Possible duplicate of [How to count the frequency of the elements in a list?](http://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-a-list) – Julien Oct 09 '16 at 07:30

3 Answers3

2

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 sequences have a handy .count method.

>>> [1,1,1,2,3,4,5].count(1)
3
>>> "ababababcdefg".count("b")
4
sytech
  • 29,298
  • 3
  • 45
  • 86
2

Check out collections.Counter()

import collections

def count_each(seeds,xs): 
    c = collections.Counter(xs)
    return [c[seed] for seed in seeds]
Julien
  • 13,986
  • 5
  • 29
  • 53
xulfir
  • 131
  • 3
1
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')
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – andreas Oct 09 '16 at 07:58
  • What are c and d equal to? This seems like the best answer but when I test the function it either returns 1 or none and not how many times the seed value appeared in the 2nd sequence – famguy74 Oct 09 '16 at 07:58
  • c is a variable which hold characters in seeds for first 'for loop, and d holds characters form xs for second 'for loop', and then we compare this ' c==d' , if it is true then increase the counter by 1. – Amitkumar Satpute Oct 09 '16 at 08:06
  • Thank you for explaining I tested your solution in command prompt and can confirm that it works however I'll have to adjust it to be able to return the seed values instead of printing them – famguy74 Oct 09 '16 at 08:11
  • you got your solution? :) – Amitkumar Satpute Oct 09 '16 at 11:57