python - How to find the number of instances of an item in a list of lists -


i want part of script writing this.

x=0 y=0 list=[["cat","dog","mouse",1],["cat","dog","mouse",2],["cat","dog","mouse",3]]  row=list[y] item=row[x] print list.count(item) 

the problem print 0 because isn't searching individual lists.how can make return total number of instances instead?

search per sublist, adding results per contained list sum():

sum(sub.count(item) sub in lst) 

demo:

>>> lst = [["cat","dog","mouse",1],["cat","dog","mouse",2],["cat","dog","mouse",3]] >>> item = 'cat' >>> sum(sub.count(item) sub in lst) 3 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -