So I'm trying to let my code print out the number of times a letter is being printed (space EXCLUDED) and here is my code so fardef c
def charHistogram(filename: str):
g = list(filename)
f = []
for x in g:
f.append([x, (g.count(x)*' ')])
return f
print(charHistogram(filename='Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Praesent ac sem lorem. Integer elementum
ultrices purus, sit amet malesuada tortor
pharetra ac. Vestibulum sapien nibh, dapibus
nec bibendum sit amet, sodales id justo.'))
and here is my output according to the code..
[['L', ' '], ['o', ' '], ['r', ' '], ['e', ' '], ['m', ' '], [' ', ' '], ['i', ' '], ['p', ' '], ['s', ' '], ['u', ' '], ['m', ' '], [' ', ' '], ['d', ' '], ['o', ' '], ['l', ' '], ['o', ' '], ['r', ' '], [' ', ' '], ['s', ' '], ['i', ' '], ['t', ' '], [' ', ' '], ['a', ' '], ['m', ' '], ['e', ' '], ['t', ' '], [',', ' '], [' ', ' '], ['c', ' '], ['o', ' '], ['n', ' '], ['s', ' '], ['e', ' '], ['c', ' '], ['t', ' '], ['e', ' '], ['t', ' '], ['u', ' '], ['r', ' '], [' ', ' '], ['a', ' '], ['d', ' '], ['i', ' '], ['p', ' '], ['i', ' '], ['s', ' '], ['c', ' '], ['i', ' '], ['n', ' '], ['g', ' '], [' ', ' '], ['e', ' '], ['l', ' '], ['i', ' '], ['t', ' '], ['.', ' '], [' ', ' '], ['P', ' '], ['r', ' '], ['a', ' '], ['e', ' '], ['s', ' '], ['e', ' '], ['n', ' '], ['t', ' '], [' ', ' '], ['a', ' '], ['c', ' '], [' ', ' '], ['s', ' '], ['e', ' '], ['m', ' '], [' ', ' '], ['l', ' '], ['o', ' '], ['r', ' '], ['e', ' '], ['m', ' '], ['.', ' '], [' ', ' '], ['I', ' '], ['n', ' '], ['t', ' '], ['e', ' '], ['g', ' '], ['e', ' '], ['r', ' '], [' ', ' '], ['e', ' '], ['l', ' '], ['e', ' '], ['m', ' '], ['e', ' '], ['n', ' '], ['t', ' '], ['u', ' '], ['m', ' '], [' ', ' '], ['u', ' '], ['l', ' '], ['t', ' '], ['r', ' '], ['i', ' '], ['c', ' '], ['e', ' '], ['s', ' '], [' ', ' '], ['p', ' '], ['u', ' '], ['r', ' '], ['u', ' '], ['s', ' '], [',', ' '], [' ', ' '], ['s', ' '], ['i', ' '], ['t', ' '], [' ', ' '], ['a', ' '], ['m', ' '], ['e', ' '], ['t', ' '], [' ', ' '], ['m', ' '], ['a', ' '], ['l', ' '], ['e', ' '], ['s', ' '], ['u', ' '], ['a', ' '], ['d', ' '], ['a', ' '], [' ', ' '], ['t', ' '], ['o', ' '], ['r', ' '], ['t', ' '], ['o', ' '], ['r', ' '], [' ', ' '], ['p', ' '], ['h', ' '], ['a', ' '], ['r', ' '], ['e', ' '], ['t', ' '], ['r', ' '], ['a', ' '], [' ', ' '], ['a', ' '], ['c', ' '], ['.', ' '], [' ', ' '], ['V', ' '], ['e', ' '], ['s', ' '], ['t', ' '], ['i', ' '], ['b', ' '], ['u', ' '], ['l', ' '], ['u', ' '], ['m', ' '], [' ', ' '], ['s', ' '], ['a', ' '], ['p', ' '], ['i', ' '], ['e', ' '], ['n', ' '], [' ', ' '], ['n', ' '], ['i', ' '], ['b', ' '], ['h', ' '], [',', ' '], [' ', ' '], ['d', ' '], ['a', ' '], ['p', ' '], ['i', ' '], ['b', ' '], ['u', ' '], ['s', ' '], [' ', ' '], ['n', ' '], ['e', ' '], ['c', ' '], [' ', ' '], ['b', ' '], ['i', ' '], ['b', ' '], ['e', ' '], ['n', ' '], ['d', ' '], ['u', ' '], ['m', ' '], [' ', ' '], ['s', ' '], ['i', ' '], ['t', ' '], [' ', ' '], ['a', ' '], ['m', ' '], ['e', ' '], ['t', ' '], [',', ' '], [' ', ' '], ['s', ' '], ['o', ' '], ['d', ' '], ['a', ' '], ['l', ' '], ['e', ' '], ['s', ' '], [' ', ' '], ['i', ' '], ['d', ' '], [' ', ' '], ['j', ' '], ['u', ' '], ['s', ' '], ['t', ' '], ['o', ' '], ['.', ' ']]
but I want it to be like this...
e
t
s
i
a
m
r
u
l
n
o
c
d
p
b
g
h
j
v
What am I supposed to change in my code?
CodePudding user response:
How about this:
def armar_dict(filename: str):
diccionario={}
for letra in filename:
if letra in diccionario:
diccionario[letra]=diccionario[letra] 1
else:
diccionario[letra]=1
return diccionario
def order_dict (diccio):
sorted_dict = {}
sorted_keys = sorted(diccio, key=diccio.get)
for w in sorted_keys:
sorted_dict[w] = diccio[w]
return sorted_dict
def char_histogram(diccio):
for k, v in diccio.items():
print (k ": " " "*v)
I hope you like it.
CodePudding user response:
This can be achieved with:
import string
def charHistogram(filename: str):
alpha = string.ascii_lowercase
g = list(filename.lower())
chars = set(g)
f = []
for char in chars:
if char in alpha:
f.append([char, (g.count(char)*' ')])
f.sort(key=lambda x: len(x[1]), reverse=True)
return f
result = charHistogram(filename='''Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Praesent ac sem lorem. Integer elementum
ultrices purus, sit amet malesuada tortor
pharetra ac. Vestibulum sapien nibh, dapibus
nec bibendum sit amet, sodales id justo.''')
for char, bar in result:
print(char, bar)
Which gives:
e
t
s
a
i
r
u
m
o
n
l
c
d
p
b
h
g
j
CodePudding user response:
You can use:
replace_list = [(" ", ""), (".", ""), (",", "")]
def charHistogram(filename: str):
for old, new in replace_list:
filename = filename.lower().replace(old, new) # replace unwanted characters
g = list(filename)
f = {}
for x in g:
if x in f:
f[x] = " "
else:
f[x]= " "
return sorted(f.items(), key=lambda x: x[1], reverse=True) # sort
data = charHistogram(filename='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac sem lorem. Integer elementum ultrices purus, sit amet malesuada tortor pharetra ac. Vestibulum sapien nibh, dapibus nec bibendum sit amet, sodales id justo.')
for key, value in data:
print(key, value)
This gives you:
e
t
s
i
a
r
m
u
l
o
n
p
d
c
b
g
h
v
j
CodePudding user response:
I think, my approach has lesser lines :-) , though the crust is same
def charHistogram(filename: str):
filename = str.lower(filename)
occurences = {char: " " * filename.count(char) for char in filename if char.isalnum()}
occurences = sorted(occurences.items(), key=lambda item: item[1], reverse=True)
occurences = "\n".join([f"{l} {c}" for l, c in occurences])
return occurences
CodePudding user response:
Here is another way to achieve this, looks much simpler to read.
from collections import Counter
test_str ='Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent ac sem lorem. Integer elementum ultrices purus, sit amet malesuada
tortor pharetra ac. Vestibulum sapien nibh, dapibus nec bibendum sit amet,
sodales id justo.'
str_count = Counter(test_str)
#sort by occurrences
str_count =str_count.most_common()
for ch in str_count:
if str(ch[0]) != ' ':
plusCtr=0
pStr=''
while plusCtr< ch[1]:
pStr =' '
plusCtr =1
print(str(ch[0]) ':' pStr)
print('end of program')