Python 按字母顺序排列句子,并计算每个单词在表格中出现和打印的次数
我正在努力解决问题中表格部分的打印问题。到目前为止,我已经设法按字母顺序排列用户输入的句子,并计算每个单词出现的次数。代码如下:Python 按字母顺序排列句子,并计算每个单词在表格中出现和打印的次数,python,python-2.7,Python,Python 2.7,我正在努力解决问题中表格部分的打印问题。到目前为止,我已经设法按字母顺序排列用户输入的句子,并计算每个单词出现的次数。代码如下: thestring = (raw_input()) sentence = thestring.split(" ") sentence.sort() count = {} for word in thestring.split(): try: count[word] += 1 except KeyError: count[word] = 1 prin
thestring = (raw_input())
sentence = thestring.split(" ")
sentence.sort()
count = {}
for word in thestring.split():
try: count[word] += 1
except KeyError: count[word] = 1
print sentence
print count
当我运行代码时,我得到以下信息:
['apple', 'apple', 'banana', 'mango', 'orange', 'pear', 'pear', 'strawberry']
{'apple': 2, 'pear': 2, 'strawberry': 1, 'mango': 1, 'orange': 1, 'banana': 1}
但是,理想情况下,我希望它打印在一个类似以下内容的表格中:
apple.....|.....2
banana....|.....1
mango.....|.....1
orange....|.....1
pear......|.....2
strawberry|.....1
谢谢你的帮助
计数器
是更好的解决方案,但是如果您想坚持您的代码,只需将打印计数
替换为
for c in sorted(count):
print c + '.'*(10-len(c))+'|'+'.'*(6-len(str(count[c])))+str(count[c])
计数器
是更好的解决方案,但是如果您想坚持您的代码,只需将打印计数
替换为
for c in sorted(count):
print c + '.'*(10-len(c))+'|'+'.'*(6-len(str(count[c])))+str(count[c])
您应该使用
集合。计数器
:
from collections import Counter
thestring = (raw_input()).split(" ")
cnt = Counter(thestring)
items = cnt.items()
items.sort(key=lambda x: x[0])
print items
您应该使用
集合。计数器
:
from collections import Counter
thestring = (raw_input()).split(" ")
cnt = Counter(thestring)
items = cnt.items()
items.sort(key=lambda x: x[0])
print items
是打印格式化字符串的Python方式:
d = {'apple': 2, 'pear': 2, 'strawberry': 1, 'mango': 1, 'orange': 1, 'banana': 1}
for word, count in sorted(d.items()):
print "{:20} | {:5}".format(word, count)
要自动调整第一列的大小,请执行以下操作:
maxlen = max(map(len, d))
for word, count in sorted(d.items()):
print "{:{}} | {}".format(word, maxlen, count)
如果你真的想用圆点(或其他什么东西)填满它,那么像这样:
for word, count in sorted(d.items()):
print "{:.<{}}|{:.>5}".format(word, maxlen, count)
对于word,按排序(d.items())计数:
打印“{.5}”。格式(word、maxlen、count)
苹果公司
香蕉
芒果
橙色……|……..1
梨
草莓……1
如前所述,对于第一部分,最好使用。是打印格式化字符串的python方法:
d = {'apple': 2, 'pear': 2, 'strawberry': 1, 'mango': 1, 'orange': 1, 'banana': 1}
for word, count in sorted(d.items()):
print "{:20} | {:5}".format(word, count)
要自动调整第一列的大小,请执行以下操作:
maxlen = max(map(len, d))
for word, count in sorted(d.items()):
print "{:{}} | {}".format(word, maxlen, count)
如果你真的想用圆点(或其他什么东西)填满它,那么像这样:
for word, count in sorted(d.items()):
print "{:.<{}}|{:.>5}".format(word, maxlen, count)
对于word,按排序(d.items())计数:
打印“{.5}”。格式(word、maxlen、count)
苹果公司
香蕉
芒果
橙色……|……..1
梨
草莓……1
正如已经指出的,对于第一部分,最好使用
其中l是u最长键的长度
在输出上,您有:
apple.....|.........2
pear......|.........2
strawberry|.........1
mango.....|.........1
orange....|.........1
banana....|.........1
其中l是u最长键的长度
在输出上,您有:
apple.....|.........2
pear......|.........2
strawberry|.........1
mango.....|.........1
orange....|.........1
banana....|.........1
输出
apple.....|....2
banana....|....1
mango.....|....1
orange....|....1
pear......|....2
strawberry|....1
您可以使用来了解它是如何工作的
输出
apple.....|....2
banana....|....1
mango.....|....1
orange....|....1
pear......|....2
strawberry|....1
您可以使用来理解它是如何工作的。但问题仍然是,如何用
s而不是空格填充格式。这似乎比rjust和ljust更有趣。投票结果:)@thg435真棒:)我每天都从你这样的人那里学习。非常感谢:)但问题仍然是,如何用
s而不是空格填充格式。这似乎比rjust和ljust更有趣。投票结果:)@thg435真棒:)我每天都从你这样的人那里学习。非常感谢:)这个答案值得接受。若你们只需添加有关计数器的信息,我将向上投票!:)@GamesBrainiac Done:)据我所知,他们希望对输出进行排序。@thg435抱歉。我错过了。现在更新了我的解决方案。请检查。这个答案应该被接受。若你们只需添加有关计数器的信息,我将向上投票!:)@GamesBrainiac Done:)据我所知,他们希望对输出进行排序。@thg435抱歉。我错过了。现在更新了我的解决方案。请查收。