Counting characters in a sentence - python

I'm trying to create a program which outputs how many times a specific character was used.
For example, if the sentence is I like bikes!
The output should be:
I = 1
l = 1
i = 2
k = 2
e = 2
b = 1
s = 1
! = 1
However my program does this
I = 1
l = 1
i = 2
k = 1
e = 2
b = 1
i = 2
k = 2
e = 2
s = 1
! = 1
So its doubling up the letters.
def count_char(text):
for char in text:
count = text.count(char)
print(char + ' = ' + str(count))
How can I fix this?

Instead of going through all the characters in text, go through all the characters in the alphabet, but report only those that are present:
def count_char(text):
for char in string.ascii_letters:
count = text.count(char)
if count:
print(char + ' = ' + str(count))

I suggest initializing a dictionary and updating the values as you run the for loop
def count_char(text):
answer={}
for char in text:
if char in answer:
answer[char]+=1
else:
answer[char]=1
print(answer)
this should give you the desired answer

If you want your output it ordered, you can derive a class from OrderedDict and Counter. If order does not matter, you can use Counter directly (as suggested by #user3483203 in a comment to your question).
from collections import OrderedDict, Counter
class OrderedCounter(Counter, OrderedDict):
pass
s = "I like bikes!"
c = OrderedCounter(s)
for char, count in c.items():
if char.strip(): # skip blanks
print(char + ' = ' + str(count))
Output:
I = 1
l = 1
i = 2
k = 2
e = 2
b = 1
s = 1
! = 1

You can try this:
def char_counter_printer(string):
if type(string) is not str:
return False
else:
chardict={}
charlist=[]
for char in string:
try:
chardict[char]+=1
except:
chardict[char]=1
charlist.append(char)
for ch in charlist:
if(ch.strip()):
print(ch,'=',chardict[ch])
char_counter_printer('I Like bikes!')
the output is:
I = 1
L = 1
i = 2
k = 2
e = 2
b = 1
s = 1
! = 1
as expected.

Related

how to recursively remove all adjacent characters that have repeated 3 or more times using python

Test Cases
Input: abbbaaccada
Output: ccada
Input: bbccdddcb
Output: (Empty string)
str = input("Enter string: ")
def my_string(string):
if not string:
return ""
if len(string) == 1:
return string
if string[0] == string[1] == string[2]:
return my_string(string[3:])
return string[0] + my_string(string[1:])
print (my_string(str))
I am new to python. and I am trying to remove characters with 3 or more consecutive appearance in a string. In this I could only able to get output of only 1 iteration. e.g. i/p- hhhelllo o/p-eo but for i/p- abbbaaccada o/p is aaaccada but it should be ccada.. please help..
I have done this till 3 repetition but how to generalize it for more than 3 repetition.??
Your problem presents the opportunity to show how else in for loops can be useful. Take a look:
def remover(my_str):
temp = set(my_str)
while True:
for c in temp:
if 3*c in my_str:
my_str = my_str.replace(3*c, '')
break
else:
break
return my_str
test1 = 'abbbaaccada'
print(remover(test1)) # -> ccada
test2 = 'i/p- hhhelllo'
print(remover(test2)) # -> i/p- eo
If you insist on having recursive calls, you can modify the above as follows:
def remover(my_str):
temp = set(my_str)
new_str = my_str
for c in temp:
if 3*c in new_str:
new_str = new_str.replace(3*c, '')
if my_str == new_str:
return new_str
else:
return remover(new_str)
I have added a solution which will work for 3 or more repetition as the above solution didn't work for me. It is a recursive solution.
import re
def format_string(u_str):
f_str = remove_string(u_str)
if f_str == u_str:
return f_str
else:
return format_string(f_str)
def remove_string(u_str):
index = 0 # This will maintain the index while traversing the entire string
while index < len(u_str):
r = re.search(u_str[index]*4 + '*', u_str)
if r:
start, end = r.span() # start and end index of substring matching 3 or more repetition
u_str = u_str[:start] + u_str[end:] # removing the found substring
index = end
else:
index += 1
return u_str
test1 = 'abbbaaccada'
print('output:' + format_string(test1))
test2 = 'bbccdddcb'
print('output:' + format_string(test2))

Better way to print a true group in Python

I want to make a program that returns a group of True variables that i found in my program. Like this:
1 = True
2 = True
3 = False
4 = False
5 = True
What I want is to return as a print
The true numbers are: 1, 2 and 5
Edit 1:
The code is a letter counter!
eacha letter in a group has a value.
Like
a=1
b = 2
...
If a number repeats more than 4 times, that number is a true
The group would be a name. like John in an imput.
The program reads it and gives a number for each letter.
what I am using right now is this (Preatty ugly I know, but I started programing this month...), where "a" is the amount of letter a in the name, b is the amount of b in the name....
if (a + j + s) >=4:
exe1 = 1
else:
exe1 = ""
if (b + k + t) >=4:
exe2 = 2
else:
exe2 = ""
if (c + l + u) >=4:
exe3 = 3
else:
exe3 = ""
if (d + m + v) >=4:
exe4 = 4
else:
exe4 = ""
if (e + n + w) >=4:
exe5 = 5
else:
exe5 = ""
if (f + o + x) >=4:
exe6 = 6
else:
exe6 = ""
if (g + p + y) >=4:
exe7 = 7
else:
exe7 = ""
if (h + q + z) >=4:
exe8 = 8
else:
exe8 = ""
if (i + r) >=4:
exe9 = 9
else:
exe9 = ""
print("Excesses:", exe1, exe2, exe3, exe4, exe5, exe6, exe7, exe8, exe9)
Why don't you use a dictionary? Take a look in this code as an example.
dict1 = {1:'True', 2:'True', 3:'False', 4:'False', 5:'True'}
list = []
for k, v in dict1.items():
if v == 'True':
#print(k, sep=' ', end=',')
list.append(k)
print(list)
Now you the list - although inside brackets and I don't know how to remove them since they are part of the list... Maybe iteration over it again should solve the problem, although I am not a truly expert in Python.

How could I write this function the pythonic way? [duplicate]

I have a string like '....(((...((...' for which I have to generate another string 'ss(4)h5(3)ss(3)h2(2)ss(3)'.
'.' corresponds to 'ss' and the number of continous '.' is in the bracket.
'(' corresponds to 'h5' and the number of continuos '(' is in the bracket.
Currently I'm able to get the output 'ss(4)h5(3)ss(3)' and my code ignores the last two character sequences.
This is what I have done so far
def main():
stringInput = raw_input("Enter the string:")
ssCount = 0
h5Count = 0
finalString = ""
ssString = ""
h5String = ""
ssCont = True
h5Cont = True
for i in range(0, len(stringInput), 1):
if stringInput[i] == ".":
h5Cont = False
if ssCont:
ssCount = ssCount + 1
ssString = "ss(" + str(ssCount) + ")"
ssCont = True
else:
finalString = finalString + ssString
ssCont = True
ssCount = 1
elif stringInput[i] == "(":
ssCont = False
if h5Cont:
h5Count = h5Count + 1
h5String = "h5(" + str(h5Count) + ")"
h5Cont = True
else:
finalString = finalString + h5String
h5Cont = True
h5Count = 1
print finalString
main()
How to modify the code to get the desired output?
I don’t know about modifying your existing code, but to me this can be done very succinctly and pythonically using itertools.groupby. Note that I’m not sure if the 'h2' in your expected output is a typo or if it should be 'h5', which I’m assuming.
from itertools import chain, groupby
string = '....(((...((...'
def character_count(S, labels): # this allows you to customize the labels you want to use
for K, G in groupby(S):
yield labels[K], '(', str(sum(1 for c in G)), ')' # sum() counts the number of items in the iterator G
output = ''.join(chain.from_iterable(character_count(string, {'.': 'ss', '(': 'h5'}))) # joins the components into a single string
print(output)
# >>> ss(4)h5(3)ss(3)h5(2)ss(3)
#Kelvin 's answer is great, however if you want to define a function yourself, you could do it like this:
def h5ss(x):
names = {".": "ss", "(": "h5"}
count = 0
current = None
out = ""
for i in x:
if i == current:
count += 1
else:
if current is not None:
out += "{}({})".format(names[current], count)
count = 1
current = i
if current is not None:
out += "{}({})".format(names[current], count)
return out

Converting string using list in python

I've been trying to rearrange the string by reversing a particular strings consecutively from the given string input and the limit is given as input.
for example
limit is 3
if input is Hellothegamestarts
output must be Heltolhegemastastr
and it is saved in separate array
The code is:
while True:
t = int(input())
if t == 0:
break
string = raw_input()
string = string.encode('utf-8')
leng = len(string)
r = t/leng
m = []
leng = 0
for i in range(r):
if r % 2 == 0:
l = 0
l = leng + t
for i in range(t):
temp = string[l]
m.append(temp)
l = l - 1
r = r + 1
leng = leng + t
else:
l = 0
l = leng
for i in range(t):
temp = string[l]
m.append(temp)
l = l + 1
r = r + 1
leng = leng + t
print m
the output i got is [] and asks for next input for t.
Any help is appreciated.
Take the blocks in chunks of 3s, and reverse the odd ones, eg:
import re
s = 'Hellothegamestarts'
r = ''.join(
el if idx % 2 == 0 else el[::-1]
for idx, el in enumerate(re.findall('.{,3}', s))
)
# Heltolhegemastastr
Maybe you can try -
t = int(input())
if t == 0:
break;
string = raw_input()
m = ''
leng = len(string)
i = 0
while i < leng:
if (i/t) % 2 != 0:
m = m + string[i+t-1:i-1:-1]
else:
m = m + string[i:i+t]
i = i + t
print(m)
Alternatively you can try this
def myfunc(s, count):
return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])]
a='Hellothegamestarts'
lst=myfunc(a,3)
print "".join([i if lst.index(i) in range(0,len(lst),2) else i[::-1] for i in lst])
myfun i didn't write it.It's from here

Markov Model - Random word/gibberish generator

My code works fine until the random word generating. Sometimes it creates words/gibberish and sometimes it doesn't (probably going through an infinite loop). However, whenever it does create words/gibberish it doesn't seem so "random". The words would either repeat themselves or most of the words will be generating near the same character length.
The problem lies in the def genRandomWord:
import random
def getTransitions(astring):
d = {}
for i in range(len(astring)):
if astring[i:i+2] in d:
d[astring[i:i+2]] += 1
else:
d[astring[i:i+2]] = 1
#h = tuple(d.items()) #gets the indexes of the dictionary
#print(h[2][1])
if ' ' in d:
del d[' ']
return d
def getFirstLetters(astring):
d = []
for i in astring:
if i not in d:
d.append(i)
d.remove(' ')
return d
def letterCount(astring):
d = {}
for i in astring:
if i not in d.keys():
d[i] = 1
else:
d[i] +=1
d[' ']-= 1
return d
def getProb(astring):
d = {}
h = tuple(getTransitions(astring).items())
j = tuple(letterCount(astring).items())
#print("h", h)
#print()
#print()
#print("j", j)
for i in h:
for n in j:
if i[0][0] == n[0]:
d[i[0]] = i[1]/n[1]
return d
def genFletter(astring):
d = {}
r = random.random()
fl ='*'
#print("r",r)
a = getProb(astring)
suma = 0
count = -1
for i in a:
if i[0][0] == ' ':
d[i[1]] = a[i]
d = sorted(tuple(d.items()))
#print(d)
while suma < r:
count += 1
suma += d[count][1]
fl = d[count][0]
#print(suma)
return fl
def genRandomWord(astring):
h = getProb(The_List)
htrans = tuple(getProb(The_List).keys())
hprob = tuple(getProb(The_List).values())
#print(hprob)
z = genFletter(The_List)
word = z
#print(word)
fletterprob = h[' '+z]
r = random.random()
while word[-1]!= ' ':
index = 0
suma = 0
for i in range(len(htrans)):
if htrans[i][0] == word[-1]:
index = i
suma += hprob[index]
for j in range(len(hprob)):
if suma >= r:
word += htrans[index][1]
break
else:
suma += hprob[index]
return word
The_List = ' steam teams meets teems eat ate state tease test mast mates '
trans = getTransitions(The_List)
lcount = letterCount(The_List)
fletter = getFirstLetters(The_List)
transProb = getProb(The_List)
#Sorting
#print('LETTER TRANSITIONS'+'\n'+str(sorted(trans.items()))+'\n')
#print('LETTER COUNT'+'\n'+str(sorted(lcount.items()))+'\n')
#print('FIRST LETTERS'+'\n'+str(sorted(fletter))+'\n')
#print('TRANSITION PROBABILITIES'+'\n'+str(sorted(transProb.items()))+'\n')
print('LETTER TRANSITIONS'+'\n'+str(trans)+'\n')
print('LETTER COUNT'+'\n'+str(lcount)+'\n')
print('FIRST LETTERS'+'\n'+str(fletter)+'\n')
print('TRANSITION PROBABILITIES'+'\n'+str(transProb)+'\n')
#print(genFletter(The_List))
for i in range(10):
print("'"+genRandomWord(The_List)+"'")

Categories

Resources