-3

In Python, I need to write a program that:

Asks the user to enter a string text. It prints out, for every letter in text, how many times it occurs in text. In the printout, letters should appear in the order in which they appear in the text, but no letter should appear twice. For any letter, you should show the TOTAL number of times it shows up in either upper case or lower case (do not count or display upper and lower cases separately). Spaces and punctuation characters should also be counted. For example:

For input 'hello world!' it should print:

h: 1
e: 1
l: 3
o: 2
    : 1
w: 1
r: 1
d: 1
!: 1

For input 'Today it is Tuesday' it should print:

t: 3
o: 1
d: 2
a: 2
y: 2
    : 3
i: 2
s: 2
u: 1
e: 1

I'm fairly new and I'm not sure how to go about this.

4

4 回答 4

7

您提到它应该按顺序排列,因此您可以从集合文档中获取OrderedCounter配方,例如:

from collections import OrderedDict, Counter

class OrderedCounter(Counter, OrderedDict):
     'Counter that remembers the order elements are first encountered'

     def __repr__(self):
         return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))

     def __reduce__(self):
         return self.__class__, (OrderedDict(self),)

letter_counts = OrderedCounter('hello world!')
# OrderedCounter(OrderedDict([('h', 1), ('e', 1), ('l', 3), ('o', 2), (' ', 1), ('w', 1), ('r', 1), ('d', 1), ('!', 1)]))

然后突出循环:

for letter, count in letter_counts.items():
    print letter, count
于 2013-07-28T17:24:50.270 回答
4

使用collections.Counter

>>> from collections import Counter
>>> Counter('hello world!')
Counter({'l': 3, 'o': 2, '!': 1, ' ': 1, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1})

没有注意到您要保留订单。为此,您可以使用模块文档中的OrderedCounter示例:collections

>>> from collections import OrderedDict
>>> 
>>> class OrderedCounter(Counter, OrderedDict):
...     def __repr__(self):
...         return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
...     def __reduce__(self):
...         return self.__class__, (OrderedDict(self),)
... 
>>> 
>>> count = OrderedCounter('hello world!')
>>> count
OrderedCounter(OrderedDict([('h', 1), ('e', 1), ('l', 3), ('o', 2), (' ', 1), ('w', 1), ('r', 1), ('d', 1), ('!', 1)]))
于 2013-07-28T17:21:22.113 回答
0

虽然这两个答案都提供了 OrderedCounter 的很好的例子,但我仍然认为这不是问题的完整答案,因为新类应该计算转为小写的字母。所以这是我的 2 美分:

from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
     'Counter that remembers the order elements are first encountered'
     def __init__(self, iterable, **kwds):
         if "lower" in dir(iterable): it = iterable.lower()
         else: it = iterable

         return super(OrderedCounter, self).__init__(it, **kwds)

     def __repr__(self):
         return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))

     def __reduce__(self):
         return self.__class__, (OrderedDict(self),)
于 2013-07-28T17:38:53.943 回答
0

听起来像家庭作业,在这种情况下collections.Counter可能不可用,以及为什么值得提供一些指导而不是答案:

首先,您需要计算唯一值。如果您有一些输入,您如何获得唯一值?

答:用一套。

>>> sample = [1, 3, 6, 7, 7, 7, 7, 8]
>>> set(sample)
{8, 1, 3, 6, 7}
# Notice: the order has been thrown away
>>> newsample = 'LollaPAloOza'
>>> set(newsample)
{'a', 'A', 'L', 'l', 'o', 'O', 'z', 'P'}
# Notice: lowercase and uppercase are treated as different characters. 

新问题:我们如何将小写和大写一视同仁?

答:修改整个输入,使其变为小写或大写:

>>> set(newsample.lower())
{'a', 'p', 'z', 'l', 'o'}

我们现在有一组唯一值要计算。下一个问题:我们如何计算这些东西?

答:我们可以遍历集合(使用 for 循环),然后计算集合中的每个项目:

my_input = newsample.lower()
for item in set(my_input):
      print(my_input.count(item))
# the trick here is to iterate through the unique values and for each element,
# to count the item that appears in the _whole_ (now lowercase) input.

最后,我们必须创建一些数据结构来保存项目及其计数。字典将是执行此操作的好方法,或者因为您需要按照它们出现的顺序给出答案,我建议您研究构建一个列表并将每个计数与列表中的每个项目以某种方式相关联。

但是,如果您可以使用它,所有这些工作都由collections.Counter.

于 2013-07-28T17:36:22.183 回答