2016-10-10 67 views
0

我試圖爲任何給定的字符串輸入計算每個字符的出現次數,出現次數必須以升序輸出(包括數字和感嘆號) 我的代碼到目前爲止,我知道的計數器功能,但它不會輸出我想要的格式的答案,我不知道如何格式化計數器。相反,我試圖找到使用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) 
+1

你能解釋一下你在找什麼輸出準確,在計數器並不足以? – idjaw

+0

什麼,你覺得'list1。count(list1)'呢? – TigerhawkT3

+0

您是否知道「計數器」實際上是一本字典?你可以很容易地通過count來進行排序。sort_words = sorted(p.items(),key = lambda item:item [1],reverse = True)'。您可以使用print('\ n'.join('sorted_words'中的'%10s:%5d'%item)')來打印。請注意,使用'count'仍然需要您進行排序。 – MisterMiyagi

回答

-1

一種方式做,這將是您去除串子的實例和看長...

另外,您可以重新使用或正則表達式,

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) 
+1

只需使用'計數器'。這是非常過分設計的。 –

+1

另外,問題中沒有關於子字符串的數目。 –

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) 
+2

'key = lambda x:x [0]'在這裏並沒有做任何事情,因爲元組已經按照「字典順序」排序。另外,我相信OP希望根據* counts *進行升序排序,所以'lambda x:x [1]',但不清楚。 –

+0

是的,你說得對。謝謝。 – SCB

0

如果你只是想以不同的格式計數器輸出:

for key, value in Counter(list1).items(): 
    print('%s: %s' % (key, value)) 
+0

他們要求輸出爲升序 – SCB

+0

按字符值升序排列或按出現次數排序? –

-3

您可以使用列表功能突破的話apart`from收藏

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) 
+0

我很抱歉,但是這與他們已有的不同? – SCB

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

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)] 
+0

爲什麼在可以'c.most_common()[:: - 1]'時使用反轉?如果您正在迭代元素並且不想根據自己的答案創建副本,則反轉實際上纔有用。 –

+0

@PadraicCunningham:'reversed()'更具可讀性。但是,是的,爲了說明這個答案,它確實做了一個不必要的副本。 – mhawke

0

另一種方法來避免使用計數器。

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. 
相關問題