python - Why isn't this character-counter program not altering the list? -
i'm writting programme counts number of times each letter appears in text. i'm trying convert graph, print out bar graph line line. adding space above letter when countdown reaches # of times appears.
import string n=input("enter text, wish luck:") list1=[] #empty list list2=list(" "*26) #26 space list in string.ascii_uppercase:#from a-z n1=n.count(a) #counts letters list1.append(n1) #appends numbers m=max(list1) #finds occuring letter c=m+1 while c!=0: c=c-1 if c==0: print(string.ascii_uppercase) break x in list1: #suppose check every term in list1 if x >c: #x greater countdowner k=list1.index(x) #find term number list2[k]="*" #replace blank "*" elif x==c: #if x equal countdowner k=list1.index(x) #find term number list2[k]="*" #replaces term place in list2 print(''.join(list2))
the code accepts uppercase letters, , right add together letters countdown list 1 @ time. when count reaches 1 appearance, , 3 letters appear once, print * on top of 1 of letter.
sample input: hello stackoverflow
* * * * * * abcdefghijklmnopqrstuvwxyz
the problem k=list1.index(x)
can find first occurrence of x
in list1
. set loop here, using extended form of index()
:
list1.index(x, start, end)
which looks index lies in range(start, end)
the loop have contain try: ... except
block handle valueerror
exception.
but there's way handle this.
#! /usr/bin/env python string import ascii_uppercase def bargraph(data): info = data.upper() print(data) print(''.join(sorted(list(data)))) #count number of occurences of each letter in info counts = [data.count(a) in ascii_uppercase] #a single row of bar graph, total of spaces row = list(" " * 26) c in range(max(counts), 0, -1): k in range(26): if counts[k] == c: row[k] = "*" print(''.join(row)) print(ascii_uppercase) def main(): #data = input("enter text, wish luck:") info = "this test string bar graph function" bargraph(data) if __name__ == '__main__': main()
my version of programme converts string upper case, prints it, , sorts , prints 1 time again create easier check bar printing section doing it's supposed do.
it uses list comprehension build list of character counts. can made shorter using list comprehension build row
.
def bargraph(data): info = data.upper() print(data) print(''.join(sorted(list(data)))) #count number of occurences of each letter in info counts = [data.count(a) in ascii_uppercase] c in range(max(counts), 0, -1): print(''.join(["*" if counts[k] >= c else " " k in range(26)])) print(ascii_uppercase)
output both versions :)
this test string bar graph function aaabceeffgghhhiiiinnnooprrrrssssttttttu * * * *** * ** * *** * ***** ** *** *** ***** *** **** abcdefghijklmnopqrstuvwxyz
edit
i should mention there's more efficient way count occurrences of each letter. current method has scan through info string 26 times, 1 time each letter. that's bit inefficient, if there's lot of info process. it's improve scan through info once, , accumulate counts. 1 way utilize dict.
#! /usr/bin/env python string import ascii_uppercase def bargraph(data): info = data.upper() print(data) print(''.join(sorted(list(data)))) #count number of occurences of each letter in info counts = dict(zip(ascii_uppercase, 26*(0,))) c in data: if c.isupper(): counts[c] += 1 highcount = max(counts.values()) c in range(highcount, 0, -1): print(''.join([" *"[counts[k] >= c] k in ascii_uppercase])) print(ascii_uppercase) def main(): #data = input("enter text, wish luck:") info = "this test string bar graph function" bargraph(data) if __name__ == '__main__': main()
i've used little trick create row printing step more compact.
counts[k] >= c
either false
or true
.
but python lets utilize boolean values if int
values, false
== 0 , true
== 1.
thus " *"[counts[k] >= c]
results in " "
if counts[k] >= c
false
, , "*"
if it's true
.
python list graph count
No comments:
Post a Comment