Counting e's in a string, using for loop [duplicate] - python

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.

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

Instead of
count == count + 1
you need to have
count = count + 1

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.

Use filter function like this
len(filter(lambda x: x==letter, myString))

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)))

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

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)

Related

counting number of occurrence in string

I'm trying to count the number of times "bob" has occurred in a given string. this is what I tried:
s = input("give me a string:")
count = 0
for i in s:
if i=="b":
for j in s:
x=0
if j!="b":
x+=1
else:
break
if s[x+1]=="o" and s[x+2]=="b":
count+=1
print(count)
if I give the string "bob", it gives back 2, and if I give something like "jbhxbobalih", it gives back 0. I don't know why this happens. any idea?
The easiest manual count would probably use indeces and slices. The main difference between this and the much simpler s.count("bob") is that it also counts overlapping occurrences:
# s = "aboboba" -> 2
count = 0
for i, c in enumerate(s):
if s[i:i+3] == "bob":
count += 1
You can try checking 3 consecutive characters, if they are 'bob', just add our counter up, and do nothing otherwise.
Your code should be like this:
s = input("give me a string:")
count = 0
for i in range(0, len(s) - 3):
if s[i] == 'b' and s[i + 1] == 'o' and s[i + 2] == 'b':
count += 1
print(count)
100 % working this will work for all string.
import re
def check(string, sub_str):
count = 0
val = re.findall(sub_str, string)
for i in val:
count+=1
print(count)
# driver code
string = "baadbobaaaabobsasddswqbobdwqdwqsbob"
sub_str = "bob"
check(string, sub_str)
This gives the correct output.

Programming error in a basic python code

I am writing simple python code:
Question:
Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.
Solution I worked:
def match_ends(words):
for items in words:
count = 0
los = len(items)
first_char= items[0]
last_char= items[los-1]
if los >=2 and first_char is last_char:
count = count+1
else:
count = count
print count
return
def main():
print 'match_ends'
match_ends(['aba', 'xyz', 'aa', 'x', 'bbb'])
I keep on Getting answer as 1 all the time, I think it is not looping entirely. Where is the error
another more concise way to do this is just:
sum(1 for s in words if len(s) > 1 and s[0] == s[-1])
I would use the == operator to compare the characters instead of the is keyword. Also you can use the [-1] index to slice from the back to get the last character instead of essentially doing [len-1]. You are also resetting the count to 0 at the beginning of each loop (also count is already a function name, try to avoid naming a variable with the same name)
That being said, here is the same idea with a few changes for readability and fixes for the above.
def matches(words):
total = 0
for word in words:
if (len(word) > 1) and (word[0] == word[-1]):
total += 1
return total
>>> matches(['aba', 'xyz', 'aa', 'x', 'bbb'])
3
The reason is that you need to place count = 0 before the line for items in words:

How to calculate and print the least and most occurring vowels in a string?

I am required to input a string, calculate the number of vowels in that string, and then calculate the most and least occurring vowels. When the string contains no vowels at all, a message should print saying "no vowels were entered". Where there are vowels which occur the same number of times (e.g. if a and e both occur twice ), these vowels should both appear as being most or least occurring. However, if some vowels are in the string but not all, then the ones which do not appear in the sting should be ignored (rather than printing "a=0"). I think the counting part at the start is correct to an extent, but not quite. As for the most/least occurrences, I don't even know where to begin. Any help would be appreciated!
myString = str(input("Please type a sentence: ").lower())
count = [0, 0, 0, 0, 0]
for vowel in myString:
if vowel == "a" :
count[0]=count[0]+1
if vowel == "e" :
count[1]=count[1]+1
if vowel == "i" :
count[2]=count[2]+1
if vowel == "o" :
count[3]=count[3]+1
if vowel == "u" :
count[4]=count[4]+1
while count[0] > 0:
print ("acount :",count[0])
break
while count[1] > 0:
print ("ecount :",count[1])
break
while count[2] > 0:
print ("icount :",count[2])
break
while count[3] > 0:
print ("ocount :",count[3])
break
while count[4] > 0:
print ("ucount :",count[4])
break
else:
if count[0] == 0 and count[1] == 0 and count[2] == 0 and count[3] == 0 and count[4] == 0:
print ("No vowels were found")
from collections import Counter
d = Counter(input("Enter Sentence:"))
print sorted("aeiou",key=lambda x:d.get(x,0))
seems like a much easier way to do it ...
Well, you've got a list, count, with 5 counts in it. How do you find out which count is the highest? Just call max on it:
>>> count = [7, 1, 3, 10, 2]
>>> max(count)
10
Now that you know the max is 10, how do you know which letters have counts of 10?
>>> max_count = max(count)
>>> for i, n in enumerate(count):
... if n == max_count:
... # use i the same way you use it above
You should be able to figure out how to do the minimum count as well.
But there's one extra problem for minimum count: it sounds like you want the minimum that's not 0, not the absolute minimum. I'd write it like this:
>>> min_count = min(x for x in count if x>0)
… or, maybe more compactly:
>>> min_count = min(filter(bool, count))
But I'm guessing you don't understand comprehensions yet. In which case, you'll need to explicitly loop over the values, keeping track of the minimum value(s) that aren't 0. This implementation of max should help guide you in the right direction:
def my_max(iterable):
max_value = None
for value in iterable:
if max_value is None or value > max_value:
max_value = value
return max_value
All that being said, this is one of many cases where using the right data structure makes the job a lot easier. For example, if you used a dictionary instead of a list, you could replace the whole first half of your code with this:
count = dict.from_keys('aeiou', 0)
for vowel in myString:
if vowel in 'aeiou':
count[vowel] += 1
Using a defaultdict or a Counter makes it even easier; then you don't need to explicitly initialize the counts to 0.
count=[0,0,0,0,0]
myString = str(input("Please type a sentence: ").lower())
for x in mystring:
flag = 'aeiou'.find(x)
if flag>=0:
count[flag] +=1
print max(count)
here find function will try to find the 'x' from aeiou if found return position of 'x` else return -1. so in flag i will get the position else -1

Compare two strings, including duplicate letters?

I'm trying to write a function that takes two user inputs: a word and a maximum length. The function reads from a text file (opened earlier in the program), looks at all the words that fit within the maximum length given, and returns a list of words from the file that contain all of the letters in the word that the user gave. Here's my code so far:
def comparison():
otherWord = input("Enter word: ")
otherWord = list(otherWord)
maxLength = input("What is the maximum length of the words you want: ")
listOfWords = []
for line in file:
line = line.rstrip()
letterCount = 0
if len(line) <= int(maxLength):
for letter in otherWord:
if letter in line:
letterCount += 1
if letterCount == len(otherLine):
listOfWords.append(line)
return listOfWords
This code works, but my problem is that it does not account for duplicate letters in the words read from the file. For example, if I enter "GREEN" as otherWord, then the function returns a list of words containing the letters G, R, E, and N. I would like it to return a list containing words that have 2 E's. I imagine I'll also have to do some tweaking with the letterCount part, as the duplicates would affect that, but I'm more concerned with recognizing duplicates for now. Any help would be much appreciated.
You could use a Counter for the otherWord, like this:
>>> from collections import Counter
>>> otherWord = 'GREEN'
>>> otherWord = Counter(otherWord)
>>> otherWord
Counter({'E': 2, 'R': 1, 'N': 1, 'G': 1})
And then your check could look like this:
if len(line) <= int(maxLength):
match = True
for l, c in counter.items():
if line.count(l) < c:
match = False
break
if match:
listOfWords.append(line)
You can also write this without a match variable using Python’s for..else construct:
if len(line) <= int(maxLength):
for l, c in counter.items():
if line.count(l) < c:
break
else:
listOfWords.append(line)
Edit: If you want to have an exact match on character count, check for equality instead, and further check if there are any extra characters (which is the case if the line length is different).
You can use collections.Counter that also lets you perform (multi)set operations:
In [1]: from collections import Counter
In [2]: c = Counter('GREEN')
In [3]: l = Counter('GGGRREEEENN')
In [4]: c & l # find intersection
Out[4]: Counter({'E': 2, 'R': 1, 'G': 1, 'N': 1})
In [5]: c & l == c # are all letters in "GREEN" present "GGGRREEEENN"?
Out[5]: True
In [6]: c == l # Or if you want, test for equality
Out[6]: False
So your function could become something like:
def word_compare(inputword, wordlist, maxlenght):
c = Counter(inputword)
return [word for word in wordlist if maxlenght <= len(word)
and c & Counter(word) == c]

Can't get my count function to work in Python

I'm trying to create a function where you can put in a phrase such as "ana" in the word "banana", and count how many times it finds the phrase in the word. I can't find the error I'm making for some of my test units not to work.
def test(actual, expected):
""" Compare the actual to the expected value,
and print a suitable message.
"""
import sys
linenum = sys._getframe(1).f_lineno # get the caller's line number.
if (expected == actual):
msg = "Test on line {0} passed.".format(linenum)
else:
msg = ("Test on line {0} failed. Expected '{1}', but got '{2}'.".format(linenum, expected, actual))
print(msg)
def count(phrase, word):
count1 = 0
num_phrase = len(phrase)
num_letters = len(word)
for i in range(num_letters):
for x in word[i:i+num_phrase]:
if phrase in word:
count1 += 1
else:
continue
return count1
def test_suite():
test(count('is', 'Mississippi'), 2)
test(count('an', 'banana'), 2)
test(count('ana', 'banana'), 2)
test(count('nana', 'banana'), 1)
test(count('nanan', 'banana'), 0)
test(count('aaa', 'aaaaaa'), 4)
test_suite()
Changing your count function to the following passes the tests:
def count(phrase, word):
count1 = 0
num_phrase = len(phrase)
num_letters = len(word)
for i in range(num_letters):
if word[i:i+num_phrase] == phrase:
count1 += 1
return count1
Use str.count(substring). This will return how many times the substring occurs in the full string (str).
Here is an interactive session showing how it works:
>>> 'Mississippi'.count('is')
2
>>> 'banana'.count('an')
2
>>> 'banana'.count('ana')
1
>>> 'banana'.count('nana')
1
>>> 'banana'.count('nanan')
0
>>> 'aaaaaa'.count('aaa')
2
>>>
As you can see, the function is non-overlapping. If you need overlapping behaviour, look here: string count with overlapping occurrences
You're using the iteration wrong, so:
for i in range(num_letters): #This will go from 1, 2, ---> len(word)
for x in word[i:i+num_phrase]:
#This will give you the letters starting from word[i] to [i_num_phrase]
#but one by one, so : for i in 'dada': will give you 'd' 'a' 'd' 'a'
if phrase in word: #This condition doesnt make sense in your problem,
#if it's true it will hold true trough all the
#iteration and count will be
#len(word) * num_phrase,
#and if it's false it will return 0
count1 += 1
else:
continue
I guess, str.count(substring) is wrong solution, because it doesn't count overlapping substrings and test suite fails.
There is also builtin str.find method, which could be helpful for the task.
Another way :
def count(sequence,item) :
count = 0
for x in sequence :
if x == item :
count = count+1
return count
A basic question rais this times.
when u see a string like "isisisisisi" howmany "isi" do u count?
at first state you see the string "isi s isi s isi" and return 3 as count.
at the second state you see the string "isisisisisi" and counts the "i" tow times per phrase like this "isi isi isi isi isi".
In other word second 'i' is last character of first 'isi' and first character of second 'isi'.
so you have to return 5 as count.
for first state simply can use:
>>> string = "isisisisisi"
>>> string.count("isi")
3
and for second state you have to recognize the "phrase"+"anything"+"phrase" in the search keyword.
the below function can do it:
def find_iterate(Str):
i = 1
cnt = 0
while Str[i-1] == Str[-i] and i < len(Str)/2:
i += 1
cnt += 1
return Str[0:cnt+1]
Now you have many choice to count the search keyword in the string.
for example I do such below:
if __name__ == "__main__":
search_keyword = "isi"
String = "isisisisisi"
itterated_part = find_iterate(search_keyword)
c = 0
while search_keyword in String:
c += String.count(search_keyword)
String = String.replace(search_keyword, itterated_part)
print c
I do not know if a better way be in python.but I tried to do this with help of Regular Expressions but found no way.

Categories