-3

Python では、次のようなプログラムを作成する必要があります。

ユーザーに文字列テキストを入力するように求めます。テキスト内のすべての文字について、テキスト内で何回出現するかを出力します。プリントアウトでは、文字はテキストに表示される順序で表示されますが、文字が 2 回表示されることはありません。どの文字についても、大文字または小文字のいずれかで表示される合計回数を表示する必要があります (大文字と小文字を別々に数えたり、表示したりしないでください)。スペースと句読点もカウントする必要があります。例えば:

入力用 'hello world!' それは印刷する必要があります:

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

「今日は火曜日です」と入力すると、次のように出力されます。

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

私はかなり新しいので、どうすればいいのかわかりません。

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

宿題のように聞こえますが、その場合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 に答える