Python 3.0 - 如何输出计数最多的字符?
Python 3.0 - How do I output which character is counted the most?
所以我能够创建一个程序来计算我计算机上的文本文件中元音(特别是 e i o)的数量。但是,我终生无法弄清楚如何显示哪一个出现得最多。我以为我会说
for ch in 'i':
return numvowel?
我只是不太确定这一步是什么。
我基本上希望它最后输出 "The letter, i, occurred the most in the text file"
def vowelCounter():
inFile = open('file.txt', 'r')
contents = inFile.read()
# variable to store total number of vowels
numVowel = 0
# This counts the total number of occurrences of vowels o e i.
for ch in contents:
if ch in 'i':
numVowel = numVowel + 1
if ch in 'e':
numVowel = numVowel + 1
if ch in 'o':
numVowel = numVowel + 1
print('file.txt has', numVowel, 'vowel occurences total')
inFile.close()
vowelCounter()
如果你想显示哪一个出现的次数最多,你必须记录 每个 个单独的元音,而不是像你所做的那样只记录 1 个总数。
保留 3 个单独的计数器(您关心的 3 个元音中的每一个一个),然后您可以通过将它们相加得到总数,或者如果您想找出哪个元音出现次数最多,您可以简单地比较这 3 个元音柜台一探究竟。
尝试使用正则表达式;
https://docs.python.org/3.5/library/re.html#regular-expression-objects
import re
def vowelCounter():
with open('file.txt', 'r') as inFile:
content = inFile.read()
o_count = len(re.findall('o',content))
e_count = len(re.findall('e',content))
i_count = len(re.findall('i',content))
# Note, if you want this to be case-insensitive,
# then add the addition argument re.I to each findall function
print("O's: {0}, E's:{1}, I's:{2}".format(o_count,e_count,i_count))
vowelCounter()
你可以这样做:
vowels = {} # dictionary of counters, indexed by vowels
for ch in contents:
if ch in ['i', 'e', 'o']:
# If 'ch' is a new vowel, create a new mapping for it with the value 1
# otherwise increment its counter by 1
vowels[ch] = vowels.get(ch, 0) + 1
print("'{}' occured the most."
.format(*[k for k, v in vowels.items() if v == max(vowels.values())]))
Python 声称拥有 "batteries included",这是一个 class 的典型案例。 class collections.Counter
做的差不多。
from collections import Counter
with open('file.txt') as file_
counter = Counter(file_.read())
print 'Count of e: %s' % counter['e']
print 'Count of i: %s' % counter['i']
print 'Count of o: %s' % counter['o']
让vowels = 'eio'
,然后
{ i: contents.count(i) for i in vowels }
对于 vowels
中的每个项目,计算在 contents
中出现的次数,并将其添加为结果词典的一部分(注意理解中的环绕大括号)。
所以我能够创建一个程序来计算我计算机上的文本文件中元音(特别是 e i o)的数量。但是,我终生无法弄清楚如何显示哪一个出现得最多。我以为我会说
for ch in 'i':
return numvowel?
我只是不太确定这一步是什么。 我基本上希望它最后输出 "The letter, i, occurred the most in the text file"
def vowelCounter():
inFile = open('file.txt', 'r')
contents = inFile.read()
# variable to store total number of vowels
numVowel = 0
# This counts the total number of occurrences of vowels o e i.
for ch in contents:
if ch in 'i':
numVowel = numVowel + 1
if ch in 'e':
numVowel = numVowel + 1
if ch in 'o':
numVowel = numVowel + 1
print('file.txt has', numVowel, 'vowel occurences total')
inFile.close()
vowelCounter()
如果你想显示哪一个出现的次数最多,你必须记录 每个 个单独的元音,而不是像你所做的那样只记录 1 个总数。
保留 3 个单独的计数器(您关心的 3 个元音中的每一个一个),然后您可以通过将它们相加得到总数,或者如果您想找出哪个元音出现次数最多,您可以简单地比较这 3 个元音柜台一探究竟。
尝试使用正则表达式; https://docs.python.org/3.5/library/re.html#regular-expression-objects
import re
def vowelCounter():
with open('file.txt', 'r') as inFile:
content = inFile.read()
o_count = len(re.findall('o',content))
e_count = len(re.findall('e',content))
i_count = len(re.findall('i',content))
# Note, if you want this to be case-insensitive,
# then add the addition argument re.I to each findall function
print("O's: {0}, E's:{1}, I's:{2}".format(o_count,e_count,i_count))
vowelCounter()
你可以这样做:
vowels = {} # dictionary of counters, indexed by vowels
for ch in contents:
if ch in ['i', 'e', 'o']:
# If 'ch' is a new vowel, create a new mapping for it with the value 1
# otherwise increment its counter by 1
vowels[ch] = vowels.get(ch, 0) + 1
print("'{}' occured the most."
.format(*[k for k, v in vowels.items() if v == max(vowels.values())]))
Python 声称拥有 "batteries included",这是一个 class 的典型案例。 class collections.Counter
做的差不多。
from collections import Counter
with open('file.txt') as file_
counter = Counter(file_.read())
print 'Count of e: %s' % counter['e']
print 'Count of i: %s' % counter['i']
print 'Count of o: %s' % counter['o']
让vowels = 'eio'
,然后
{ i: contents.count(i) for i in vowels }
对于 vowels
中的每个项目,计算在 contents
中出现的次数,并将其添加为结果词典的一部分(注意理解中的环绕大括号)。