的问题是,k=list1.index(x)
只能找到list1
的x
第一次出现。所以,你可以把一个循环这里,使用的index()
扩展形式:
list1.index(x, start, end)
其中只查找出在范围内的折射率(开始,结束)
这个循环必须包含一个try: ... except
块来处理ValueError
异常。
但还有另一种处理方法。
#! /usr/bin/env python
from string import ascii_uppercase
def bargraph(data):
data = data.upper()
print(data)
print(''.join(sorted(list(data))))
#Count number of occurences of each letter in data
counts = [data.count(a) for a in ascii_uppercase]
#A single row of the bar graph, initially full of spaces
row = list(" " * 26)
for c in range(max(counts), 0, -1):
for k in range(26):
if counts[k] == c:
row[k] = "*"
print(''.join(row))
print(ascii_uppercase)
def main():
#data = input("Enter Text, I wish you luck:")
data = "This is a test string for the bar graph function"
bargraph(data)
if __name__ == '__main__':
main()
我你的程序的版本字符串转换为大写,打印,然后再排序,并打印出来,使其更容易检查,酒吧印刷部是做什么它应该做的事。
它使用列表理解来构建字符计数列表。通过使用列表理解来构造row
可以使其更短。
def bargraph(data):
data = data.upper()
print(data)
print(''.join(sorted(list(data))))
#Count number of occurences of each letter in data
counts = [data.count(a) for a in ascii_uppercase]
for c in range(max(counts), 0, -1):
print(''.join(["*" if counts[k] >= c else " " for k in range(26)]))
print(ascii_uppercase)
输出两个版本:)
THIS IS A TEST STRING FOR THE BAR GRAPH FUNCTION
AAABCEEFFGGHHHIIIINNNOOPRRRRSSSSTTTTTTU
*
*
* ***
* ** * ***
* ***** ** ***
*** ***** *** ****
ABCDEFGHIJKLMNOPQRSTUVWXYZ
编辑
我应该指出,有计算每个字母的出现更有效的方式。目前的方法必须扫描数据字符串26次,每个字母一次。这有点低效,尤其是在需要处理大量数据的情况下。所以最好只扫描一次数据,并累计计数。一种方法是使用字典。
#! /usr/bin/env python
from string import ascii_uppercase
def bargraph(data):
data = data.upper()
print(data)
print(''.join(sorted(list(data))))
#Count number of occurences of each letter in data
counts = dict(zip(ascii_uppercase, 26*(0,)))
for c in data:
if c.isupper():
counts[c] += 1
highcount = max(counts.values())
for c in range(highcount, 0, -1):
print(''.join([" *"[counts[k] >= c] for k in ascii_uppercase]))
print(ascii_uppercase)
def main():
#data = input("Enter Text, I wish you luck:")
data = "This is a test string for the bar graph function"
bargraph(data)
if __name__ == '__main__':
main()
我也使用了一个小技巧,使行打印步骤更加紧凑。
counts[k] >= c
将是False
或True
。
但Python允许我们使用布尔值,就好像它们是int
值,其中False
== 0和True
== 1。
因此" *"[counts[k] >= c]
结果" "
如果counts[k] >= c
为False
,并"*"
如果是True
。
非常感谢! :d – 2014-10-22 18:52:33