Matthew Hanson
Matthew Hanson

Reputation: 331

python counting letters in string without count function

I am trying to write a program to count the occurrences of a specific letter in a string without the count function. I made the string into a list and set a loop to count but the count is never changing and i cant figure out why. This is what I have right now:

letter = 'a'
myString = 'aardvark'
myList = []

for i in myString:
    myList.append(i)

count = 1

for i in myList:
    if i == letter:
        count == count + 1

    else:
        continue

print (count)

Any help is greatly appreciated.

Upvotes: 2

Views: 19783

Answers (8)

Navieen
Navieen

Reputation: 1

For the code to function properly, two alterations need to be made:

  1. Modify the initialization of count
count = 0
  1. Use = instead of ==
count = count + 1

or

count += 1

Upvotes: 0

vignesh
vignesh

Reputation: 1

Your code logic is right except for considering count == 1 while using count == 1 you are comparing if count == 1 and count = 1 is for assigning and count += 1 is for incrementing.
Probably you know this, you might have got confused also, you have to initialize count = 0

letter = 'a'
myString  = 'aardvark'
myList = []

for i in myString:
    myList.append(i)
count = 0
for i in myList:
    if i == letter:
        count +=1
    else:
        continue
print(count)

Upvotes: 0

Prajwal KV
Prajwal KV

Reputation: 61

Apart from the above methods, another easiest way is to solve the problem by using the python dictionary

 word="purple"
 dic={}
 for letter in word:
     if letter in dic:
        dic[letter]+=1
     else:
        dic[letter]=1
 print(dic)
 {'p': 2, 'u': 1, 'r': 1, 'l': 1, 'e': 1}

In case if you want to count the occurences of a particular character in the word.we can get that it by following the below mentioned way,

        dic['p']
        2  

Upvotes: 1

TheBlackCat
TheBlackCat

Reputation: 10298

Although someone else has solved your problem, the simplest solution to do what you want to do is to use the Counter data type:

>>> from collections import Counter
>>> letter = 'a'
>>> myString = 'aardvark'
>>> counts = Counter(myString)
>>> print(counts)
Counter({'a': 3, 'r': 2, 'v': 1, 'k': 1, 'd': 1})
>>> count = counts[letter]
>>> print(count)
3

Or, more succinctly (if you don't want to check multiple letters):

>>> from collections import Counter
>>> letter = 'a'
>>> myString = 'aardvark'
>>> count = Counter(myString)[letter]
>>> print(count)
3

The simplest way to do your implementation would be:

count = sum(i == letter for i in myString)

or:

count = sum(1 for i in myString if i == letter)

This works because strings can be iterated just like lists, and False is counted as a 0 and True is counted as a 1 for arithmetic.

Upvotes: 1

HavelTheGreat
HavelTheGreat

Reputation: 3386

Your count is never changing because you are using == which is equality testing, where you should be using = to reassign count. Even better, you can increment with

count += 1

Also note that else: continue doesn't really do anything as you will continue with the next iteration of the loop anyways. If I were to have to come up with an alternative way to count without using the count function, I would lean towards regex:

import re
stringy = "aardvark"
print(len(re.findall("a", stringy)))

Upvotes: 0

Josip Grggurica
Josip Grggurica

Reputation: 421

Use filter function like this

len(filter(lambda x: x==letter, myString))

Upvotes: 0

sray
sray

Reputation: 584

Instead of

count == count + 1

you need to have

count = count + 1

Upvotes: 1

Yuri Malheiros
Yuri Malheiros

Reputation: 1410

Be careful, you are using count == count + 1, and you must use count = count + 1

The operator to attribute a new value is =, the operator == is for compare two values

Upvotes: 2

Related Questions