FAQ: Learn Python - Practice Makes Perfect - count

>>> def count(sequence, item):
...     return str(sequence).count(str(item))
... 
>>> count([11, 1], 1)
3

How is str helping?

It’s not, it changes the representation into something that is no longer the same. This isn’t text.
You can even fix it by only pressing backspace:

>>> def count(sequence, item):
...     return sequence.count(item)
... 
>>> count([11, 1], 1)
1

That still leaves the problem of that it doesn’t implement count, it only uses another count.

1 Like

i see where you are going. well, removing str() does not count lists inside lists :confused:

Should it? Is there a need to? Does it help? Is there a reason why you would need to use nested lists as opposed to sending in a flat list?

deep_count = count(flatten(stuff), 1)

str doesn’t help because the things counted aren’t text. it can be immediately ruled out as helpful.

What did you mean to do? Write that. If it’s some possible action, then, describe it, then run it. Right? Just be sure to stick relentlessly to what you meant.

Also, if you have a separate flatten then you can use it for other things than count, and they too also won’t have to care about nested lists. Counting and traversing can be two separate things.

1 Like

Well, there was excersize while learning python 2 and ot asked to count stuff inside list. So when i did first way it gave error, so i tried differently and it worked after that… but it seems u have t o do something entirely different of u want to do deep count…

I’m not clear why one would go to the trouble to right a sequence item count tool if it is only a wrapper for a string tool. What will your tool do with this?

>>> a = {1:1, 2:2, 3:3, 4:4, 5:4, 6:4}
>>> count(a.values(), 4)
3
>>> 

Don’t answer, it returns 3. However I’ll not buy into the str() thing when I don’t see how it is something worth stressing over or even defending. It’s redundant, and when we suspect that something will come along that will trip it up, doesn’t help assuaging our concerns. It doesn’t make enough sense apart from arguing the mechanics work so it should be an accepted pattern.

The biggest concern is that you haven’t written your own algorithm for this problem but would argue in favor of something that was advised against in the first place.

You aren’t counting int, you are counting the occurences of a particular int in a list of int; quite legal.

The top code is the solution given and the bottom code was my attempt at a solution: Why can I not pass a list into sequence in the bottom code? I can only seem to pass a string. I wanted to take whatever list was given and separate it out to look for the item, instead with my code all you can do is a string. Also, not sure how the solution given works, why does it find item when I should only be one place of the sequence, in other words, if the sequence is 111111,11111,1,14115, and the item is 11 how does it count thru this, there is really no 11 in this sequence I would think with this code it would find ever time there is a 11 at all. Just looking to understand why this code was so simple, did not think the solution would be that easy.

def count(sequence, item):
count = 0
for i in sequence:
if i == item:
count += 1
return count

print count([“as”, “much”, “as”, “you”], “as”)

def count(sequence , item):
word = sequence.split(",")
num = 0
print word
for i in word:
if i == item:
num += 1
return num

print count(“8,4,6,8,4”,“4”)

Hello @core0852353437!

This is because you are calling the .split() method on sequence. .split() is a method that can only be used for strings, so the compiler assumes that sequence is a string and not a list.

2 Likes