counting the frequency of a letter in a string - python-3.x

so far this is what i have and this and this will just print out a list with the count of each letter in the string. it only checks for lowercase letters.
`S="""Four score and seven years ago our fathers brought forth on this continent a new nation
Now we are engaged in a great civil war
"""
lowlet = S.lower()
L_count = [0]*26
total_count = 0
alpha = "abcdefghijklmnopqrstuvwxyz"
i = 0
while i < len(lowlet):
if lowlet[i] in alpha:
L_count[i][ord(lowlet[i]) - 97] += 1
total_count += 1
i += 1
print('total count of letters:',total_count)'
now im giving this algorithm but i cant put it into code and i cant use a for loop i have to use a while loop
Initialize a list, L_freq.
For each element, count, in L_counts
Find the letter corresponding to this count
Insert in L_freq, the list: [count, letter]

is it a requirement that it be a list? I feel like a dictionary would be easier to handle
sentence = s.lower()
counts = { letter: sentence.count(letter) for letter in alpha }
print(counts)
this will print like:
{'a': 5, 'b': 2}

Related

Check how many consecutive times appear in a string

I want to to display a number or an alphabet which appears mostly
consecutive in a given string or numbers or both.
Example:
s= 'aabskeeebadeeee'
output: e appears 4 consecutive times
I thought about set the string then and for each element loop the string to check if equal with element set element if so count =+1 and check if next to it is not equal add counter value to list with same index as in set, if is add counter value to li list if value is bigger than existing.
The problem is error index out or range although I think I am watching it.
s = 'aabskeeebadeeee'
c = 0
t = list(set(s)) # list of characters in s
li=[0,0,0,0,0,0] # list for counted repeats
print(t)
for x in t:
h = t.index(x)
for index, i in enumerate(s):
maximus = len(s)
if i == x:
c += 1
if index < maximus:
if s[index +1] != x: # if next element is not x
if c > li[h]: #update c if bigger than existing
li[h] = c
c = 0
else:
if c > li[h]:
li[h] = c
for i in t:
n = t.index(i)
print(i,li[n])
print(f'{s[li.index(max(li))]} appears {max(li)} consecutive times')
Here is an O(n) time, O(1) space solution, that breaks ties by returning the earlier seen character:
def get_longest_consecutive_ch(s):
count = max_count = 0
longest_consecutive_ch = previous_ch = None
for ch in s:
if ch == previous_ch:
count += 1
else:
previous_ch = ch
count = 1
if count > max_count:
max_count = count
longest_consecutive_ch = ch
return longest_consecutive_ch, max_count
s = 'aabskeeebadeeee'
longest_consecutive_ch, count = get_longest_consecutive_ch(s)
print(f'{longest_consecutive_ch} appears {count} consecutive times in {s}')
Output:
e appears 4 consecutive times in aabskeeebadeeee
Regex offers a concise solution here:
inp = "aabskeeebadeeee"
matches = [m.group(0) for m in re.finditer(r'([a-z])\1*', inp)]
print(matches)
matches.sort(key=len, reverse=True)
print(matches[0])
This prints:
['aa', 'b', 's', 'k', 'eee', 'b', 'a', 'd', 'eeee']
eeee
The strategy here is to find all islands of similar characters using re.finditer with the regex pattern ([a-z])\1*. Then, we sort the resulting list descending by length to find the longest sequence.
Alternatively, you can leverage the power of itertools.groupby() to approach this type of problem (for quick counting for similar items in groups. [Note, this can be applied to some broader cases, eg. numbers]
from itertools import groupby
>>> char_counts = [str(len(list(g)))+k for k, g in groupby(s)]
>>> char_counts
['2a', '1b', '1s', '1k', '3e', '1b', '1a', '1d', '4e']
>>> max(char_counts)
'4e'
# you can continue to do the rest of splitting, or printing for your needs...
>>> ans = '4e' # example
>>> print(f' the most frequent character is {ans[-1]}, it appears {ans[:-1]} ')
Output:
the most frequent character is e, it appears 4
This answer was posted as an edit to the question Check how many consecutive times appear in a string by the OP Ziggy Witkowski under CC BY-SA 4.0.
I did not want to use any libraries.
s = 'aabskaaaabadcccc'
lil = tuple(set(s)) # Set a characters in s to remove duplicates and
then make a tuple
li=[0,0,0,0,0,0] # list for counted repeats, the index of number
repeats for character
# will be equal to index of its character in a tuple
for i in lil: #iter over tuple of letters
c = 0 #counter
h= lil.index(i) #take an index
for letter in s: #iterate ove the string characters
if letter == i: # check if equal with character from tuple
c += 1 # if equal Counter +1
if c > li[lil.index(letter)]: # Updated the counter if present is bigger than the one stored.
li[lil.index(letter)] = c
else:
c=0
continue
m = max(li)
for index, j in enumerate(li): #Check if the are
characters with same max value
if li[index] == m:
print(f'{lil[index]} appears {m} consecutive times')
Output:
c appears 4 consecutive times
a appears 4 consecutive times

Problem with Python Code and the Functions in it

I have a Problem, I have to solve a task in Python and I dont know how to do it. The task is to define a function number_of_vowels, where the output should be the Number of vowels in a Word. With this function I have to write anotherone, many_vowels thats working with a list an a Number and where the number says how many vowels have to be at least in a word to be appended to the result list and then I have to append this Word. Thanks to everybody helping me ;D.
here is the code:
Wort = "parameter"
def number_of_vowels(Word):
result = 0
counter0 = 0
while result < 20:
if Word[counter0] == 'a' or 'e' or 'i' or 'o' or 'u':
result = result + 1
counter0 = counter0 + 1
else:
counter0 = counter0 + 1
return result
Words = []
counter1 = 0
def many_vowels(List , number):
if number_of_vowels(List[counter1]) < number:
counter1 + 1
else:
Words.append(List[counter1])
counter1 + 1
return Words
This code just gives me the answer to the letter a and not to the other vowels. For
print(number_of_vowels(Wort))
the output is: 1
but there are 4 vowels in this word
it also says: line 21, in many_vowels
IndexError: string index out of range
You're trying to call a function with wrong brackets. Function call should use round ones.
Try changing number_of_vowels[List[counter1]] with number_of_vowels(List[counter1])
This code contains some errors:
Calling for function should be using round brackets: number_of_vowels(List[counter1]) instead of number_of_vowels[List[counter1]]
doing result + 1 won't change value of the variable result, since you did not put the calculation result in the variable. use result = result + 1 (same for counters)
in number_of_vowels function, you want to scan the whole word? cause you did not use any loop, so it currently looking only at the first letter. Secondly, you put the compression in result and then add 1 to it. I'm not really sure why
edit:
Word = "parameter"
def number_of_vowels(Word):
result = 0
counter0 = 0
for index, letter in enumerate(Word):
if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u':
result = result + 1
return result
Words = []
counter1 = 0
def many_vowels(List_name , number):
for index, item in enumerate (List_name):
if number_of_vowels(item) >= number:
Words.append(item)
return Words

Count the no of occurence of a each string and print the eac number and string side by side using python

USING PYTHON3
So this is my question, say I have a string
my_str="aaaaabbbbcccdde"
I want to write a code that gives me an output as
5a4b3c2d1e
I saw a code in the book automate the boring stuff that does something similar but not exactly.
This is the code below:
my_str="aaaaabbbbcccdde"
count = {}
for character in my_str:
count.setdefault(character, 0)
count[character]+= 1
print(count)
for item in count:
print(count[item], item)
and this is the output it gives me:
{'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1}
5 a
4 b
3 c
2 d
1 e
not
5a4b3c2d1e
which is the output I want.
Help anyone? (:
You can do something like this brother by modifying your own code's output. Happy coding!
my_str="aaaaabbbbcccdde"
count = {}
for character in my_str:
count.setdefault(character, 0)
count[character]+= 1
string_to_print = ""
for item in count:
string_to_print += str(count[item])+item
print(string_to_print)
Output:
5a3c4b1e2d
You could do:
count = {}
for char in my_str:
count[char] = count.get(char,0) + 1
for item in count:
print(count[item],item,sep='', end='')
5a4b3c2d1e
if you need a one liner, you could do:
''.join([str(my_str.count(i)) + i for i in sorted(set(my_str),key = my_str.index)])
Out: '5a4b3c2d1e'
Using re:
import re
my_str="aaaaabbbbcccdde"
res=eval(re.sub(r"(.)(\1*)", r"+str(len('\2\1'))+'\1'", my_str)[1:])
Output:
5a4b3c2d1e
Caveat
It aggregates consecutive chars only i.e.
import re
my_str="aaaaabbbbcccddea"
res=eval(re.sub(r"(.)(\1*)", r"+str(len('\2\1'))+'\1'", my_str)[1:])
#outputs:
>> 5a4b3c2d1e1a
You were close. All you need to do to complete the code is to concat count[item] and item together and add it to the list. Then join all list elements into a final string.
my_str = "aaaaabbbbcccdde"
count = {}
list_1 = []
for character in my_str:
count.setdefault(character, 0)
count[character] += 1
print(count)
for item in count:
new_item = str(count[item]) + item
list_1.append(new_item)
final_string = ''.join(list_1)
print(final_string)
Output
5a4b3c2d1e
Or if you don't want to make another list you can just make an empty string variable and concat to that directly
my_str = "aaaaabbbbcccdde"
final_string = ""
count = {}
for character in my_str:
count.setdefault(character, 0)
count[character] += 1
print(count)
for item in count:
final_string += str(count[item]) + item
print(final_string)
Output
5a4b3c2d1e

How to count the occurrency of a specific character in a list that contains sub-lists of strings?

I would need help regarding this problem. I am trying to loop the index of an element, I have a list which is made of these sub lists: ['Galagonya', 'Alfonz', 'XXXXXXN'], I am trying to count the number of "X"-s in the whole list, so I made a for loop which goes through the big lists 2nd elements(0-1-2), where these letters are, and then I would like to individually check the six characters if they are "X" or not, if yes add one to my counter. Code attached. datum_nelkul is the big list. Please help me understand how I can improve this code. I get string index out of ranger error.
.
The non-working one:
counter = 0
i = 0
for line in datum_nelkul:
if line[2][i] == "X":
counter += 1
i +=1
The working one, which only suits short, copiable amounts, but works in this case
counter = 0
for line in datum_nelkul:
if line[2][0] == "X":
counter += 1
if line[2][1] == "X":
counter += 1
if line[2][2] == "X":
counter += 1
if line[2][3] == "X":
counter+= 1
if line[2][4] == "X":
counter += 1
if line[2][5] == "X":
counter += 1
if line[2][6] == "X":
counter += 1
You could concatenate everything and do a count, if I understand how you are trying to count.
master_list=[['XXXXXX','a','b','c'],['XXXXXX','a','b','c'],['XXXXXX','a','b','c']]
master_string=''
for l in master_list:
temp_str=''
for string in l:
temp_str+=string
master_string+=temp_str
x_count=master_string.count('X')
Out: 18
Or if you are trying to find the number of X's in each substring, and save those in a list or something
master_list=[['XXXXXX','a','b','c'],['XXXXXX','a','b','c'],['XXXXXX','a','b','c']]
x_sublist_counts=[]
for l in master_list:
temp_str=''
for string in l:
temp_str+=string
x_sublist_counts.append(temp_str.count('X'))
print(x_sublist_counts)
Out: [6, 6, 6]
You can use the following one liner. Here you loop over each element of each list in your master list, then you count the occurrences of your letter in that particular element. This will results in a list of integers that corresponds to the number of occurrences that you can simply sum() to find the total number of occurrences.
x_count = sum([ master_string.count('X') for sub_list in master_list for master_string in sub_list ])
Strings are iterable in python. All you need to do is something like this :
for line in datum_nelkul:
for c in line[2]:
if c == "X":
counter += 1

Return number of alphabetical substrings within input string

I'm trying to generate code to return the number of substrings within an input that are in sequential alphabetical order.
i.e. Input: 'abccbaabccba'
Output: 2
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def cake(x):
for i in range(len(x)):
for j in range (len(x)+1):
s = x[i:j+1]
l = 0
if s in alphabet:
l += 1
return l
print (cake('abccbaabccba'))
So far my code will only return 1. Based on tests I've done on it, it seems it just returns a 1 if there are letters in the input. Does anyone see where I'm going wrong?
You are getting the output 1 every time because your code resets the count to l = 0 on every pass through the loop.
If you fix this, you will get the answer 96, because you are including a lot of redundant checks on empty strings ('' in alphabet returns True).
If you fix that, you will get 17, because your test string contains substrings of length 1 and 2, as well as 3+, that are also substrings of the alphabet. So, your code needs to take into account the minimum substring length you would like to consider—which I assume is 3:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def cake(x, minLength=3):
l = 0
for i in range(len(x)):
for j in range(i+minLength, len(x)): # carefully specify both the start and end values of the loop that determines where your substring will end
s = x[i:j]
if s in alphabet:
print(repr(s))
l += 1
return l
print (cake('abccbaabccba'))

Resources