我正在尝试计算任何给定字符串输入的每个字符的出现次数,必须按升序输出事件(包括数字和感叹号) 到目前为止,我已经为我的代码提供了这个,我知道Counter函数,但它不会以我想要的格式输出答案,而且我不知道如何格式化Counter。相反,我试图找到使用count()来计算每个字符。我也看过字典函数,但是我希望有一种更简单的方法来用count()
from collections import Counter
sentence=input("Enter a sentence b'y: ")
lowercase=sentence.lower()
list1=list(lowercase)
list1.sort()
length=len(list1)
list2=list1.count(list1)
print(list2)
p=Counter(list1)
print(p)
答案 0 :(得分:1)
只需拨打.most_common
并使用reversed反转输出即可将输出从最不常见到最常见:
from collections import Counter
sentence= "foobar bar"
lowercase = sentence.lower()
for k, count in reversed(Counter(lowercase).most_common()):
print(k,count)
答案 1 :(得分:1)
collections.Counter
个对象提供most_common()
方法,该方法以递减频率返回元组列表。因此,如果您希望它以递增频率,请反转列表:
from collections import Counter
sentence = input("Enter a sentence: ")
c = Counter(sentence.lower())
result = reversed(c.most_common())
print(list(result))
演示
Enter a sentence: Here are 3 sentences. This is the first one. Here is the second. The end! [('a', 1), ('!', 1), ('3', 1), ('f', 1), ('d', 2), ('o', 2), ('c', 2), ('.', 3), ('r', 4), ('i', 4), ('n', 5), ('t', 6), ('h', 6), ('s', 7), (' ', 14), ('e', 14)]
答案 2 :(得分:0)
最好的办法是使用Counter
(对字符串有效),然后对其输出进行排序。
from collections import Counter
sentence = input("Enter a sentence b'y: ")
lowercase = sentence.lower()
# Counter will work on strings
p = Counter(lowercase)
count = Counter.items()
# count is now (more or less) equivalent to
# [('a', 1), ('r', 1), ('b', 1), ('o', 2), ('f', 1)]
# And now you can run your sort
sorted_count = sorted(count)
# Which will sort by the letter. If you wanted to
# sort by quantity, tell the sort to use the
# second element of the tuple by setting key:
# sorted_count = sorted(count, key=lambda x:x[1])
for letter, count in sorted_count:
# will cycle through in order of letters.
# format as you wish
print(letter, count)
答案 3 :(得分:0)
如果您只想以不同方式格式化计数器输出:
for key, value in Counter(list1).items():
print('%s: %s' % (key, value))
答案 4 :(得分:0)
避免使用Counter的另一种方法。
sentence = 'abc 11 222 a AAnn zzz?? !'
list1 = list(sentence.lower())
#If you want to remove the spaces.
#list1 = list(sentence.replace(" ", ""))
#Removing duplicate characters from the string
sentence = ''.join(set(list1))
dict = {}
for char in sentence:
dict[char] = list1.count(char)
for item in sorted(dict.items(), key=lambda x: x[1]):
print 'Number of Occurences of %s is %d.' % (item[0], item[1])
输出:
Number of Occurences of c is 1.
Number of Occurences of b is 1.
Number of Occurences of ! is 1.
Number of Occurences of n is 2.
Number of Occurences of 1 is 2.
Number of Occurences of ? is 2.
Number of Occurences of 2 is 3.
Number of Occurences of z is 3.
Number of Occurences of a is 4.
Number of Occurences of is 6.
答案 5 :(得分:-1)
执行此操作的一种方法是删除子字符串的实例并查看长度...
def nofsub(s,ss):
return((len(s)-len(s.replace(ss,"")))/len(ss))
或者你可以使用re或正则表达式,
from re import *
def nofsub(s,ss):
return(len(findall(compile(ss), s)))
最后你可以手动计算它们,
def nofsub(s,ss):
return(len([k for n,k in enumerate(s) if s[n:n+len(ss)]==ss]))
使用......
测试三者中的任何一个>>> nofsub("asdfasdfasdfasdfasdf",'asdf')
5
现在您可以计算任何给定的角色,您可以迭代字符串的唯一字符,并为您找到的每个唯一字符应用计数器。然后排序并打印结果。
def countChars(s):
s = s.lower()
d = {}
for k in set(s):
d[k]=nofsub(s,k)
for key, value in sorted(d.iteritems(), key=lambda (k,v): (v,k)):
print "%s: %s" % (key, value)
答案 6 :(得分:-3)
你可以使用list函数来破坏集合中的单词
from collections import Counter
sentence=raw_input("Enter a sentence b'y: ")
lowercase=sentence.lower()
list1=list(lowercase)
list(list1)
length=len(list1)
list2=list1.count(list1)
print(list2)
p=Counter(list1)
print(p)