LightSith
LightSith

Reputation: 927

python giving TypeError: count takes at least * argument even when arguments are given

I have following code in which I have given i as an argument to cout() but still getting TypeError: count() takes at least 1 argument (0 given)

def is_isogram(s:str):
    for i in s:
        print(i)
        if( str.count(i) > 1): # specified argument , still getting error
            return False
        return True 

Upvotes: 0

Views: 1743

Answers (1)

Chris_Rands
Chris_Rands

Reputation: 41228

str.count(i) > 1 should be s.count(i) > 1, this will fix your error.

You could use for i in set(i): or collections.Counter to solve your task more efficiently:

>>> from collections import Counter
>>> s = 'abbc'
>>> Counter(s).most_common(1)[0][1] == 1
False

Timings:

s = 'abcdefghijklmnopqrstuvwxyzz'  # a worst case?

%timeit Counter(s).most_common(1)[0][1] == 1
13.2 µs ± 27.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit len(set(s)) == len(s)
1.33 µs ± 3.46 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%%timeit
for i in set(s):
    if s.count(i) > 1:
        break
1.72 µs ± 17.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%%timeit
for i in s:
    if s.count(i) > 1:
        break
6.78 µs ± 14.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Upvotes: 1

Related Questions