Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,796 answers

573 users

How to count the occurrences of all items of list in a tuple with Python

2 Answers

0 votes
def count_occurrences(tpl, lst): 
    count = 0
    for item in tpl: 
        if item in lst: 
            count += 1
      
    return count  


tpl = (1, 2, 4, 3, 2, 1, 1, 1, 5, 6, 7, 2, 8, 9, 9) 
lst = [1, 2] 
print(count_occurrences(tpl, lst)) 



'''
run:

7

'''

 



answered Dec 19, 2019 by avibootz
0 votes
def count_occurrences(tpl, lst): 
    st = set(lst) 
    return sum(1 for n in tpl if n in st) 


tpl = (1, 2, 4, 3, 2, 1, 1, 1, 5, 6, 7, 2, 8, 9, 9) 
lst = [1, 2] 
print(count_occurrences(tpl, lst)) 



'''
run:

7

'''

 



answered Dec 19, 2019 by avibootz

Related questions

1 answer 162 views
1 answer 149 views
1 answer 143 views
2 answers 182 views
...