2017-06-05 39 views
1

我与藏品如何分隔柜台的大小写?

s = 'Hello Mr. Rogers, how are you this fine Tuesday?' 
import collections 
c = collections.Counter(s) 

结果我有思想的东西

Counter({' ': 8, 
     ',': 1, 
     '.': 1, 
     '?': 1, 
     'H': 1, 
     'M': 1, 
     'R': 1, 
     'T': 1, 
     'a': 2, 
     'd': 1, 
     'e': 5, 
     'f': 1, 
     'g': 1, 
     'h': 2, 
     'i': 2, 
     'l': 2, 
     'n': 1, 
     'o': 4, 
     'r': 3, 
     's': 3, 
     't': 1, 
     'u': 2, 
     'w': 1, 
     'y': 2}) 

如果我试图和我有语法问题

print sum(1 for i in c if i.isupper()) 

File "<ipython-input-21-66a7538534ee>", line 4 
    print sum(1 for i in c if i.isupper()) 
      ^
SyntaxError: invalid syntax 

我应该如何只计算上或从柜台下降?

+1

从你的例子是不是很明显,你需要饲料柜台只有一个事实,一封信我大写还是小写? –

+0

@JonathonReinhart是的,但如何?我的总和不起作用。 – MishaVacic

回答

3

你在你的发电机expresion缺乏()

sum((1 for x in c if x.isupper())) 
4 

编辑:由于@Błotosmęteksugest,你缺乏在打印的(),我猜你正在使用python3,你应该使用print()

+0

从错误的位置判断,他在他的'print'函数调用中缺少() - 显然他使用的是Python 3.x,所以它应该是:print(sum(1 for x in x if x.isupper() ))(不管怎样,@Nurzhan的回答可能是OP真正想要的) –

+0

@Błotosmętek,其实是的,谢谢。 – Netwave

+0

@DanielSanchez是的,我正在使用Python 3 – MishaVacic

3

你可以尝试这样的事情:

import collections 

s = 'Hello Mr. Rogers, how are you this fine Tuesday?' 
c = collections.Counter([ch for ch in s if ch.isupper()]) 
# Change to ch.islower() if you need lower case 
# c = collections.Counter([ch for ch in s if ch.islower()]) 
print(c)