4

I am just wondering how i would convert a string, such as "hello there hi there", and turn it into a dictionary, then using this dictionary, i want to count the number of each word in the dictionary, and return it in alphabetic order. So in this case it would return:

[('hello', 1), ('hi', 1), ('there', 2)]

any help would be appreciated

jamylak
  • 128,818
  • 30
  • 231
  • 230
James Pinson
  • 221
  • 1
  • 2
  • 10
  • Given a string `"hi, there!"` how would you split it into words? Given a string `维基百科` how would you split it into words? – georg May 15 '13 at 06:53

2 Answers2

14
>>> from collections import Counter
>>> text = "hello there hi there"
>>> sorted(Counter(text.split()).items())
[('hello', 1), ('hi', 1), ('there', 2)]

class collections.Counter([iterable-or-mapping])

A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

jamylak
  • 128,818
  • 30
  • 231
  • 230
4

jamylak did fine with Counter. this is a solution without importing Counter:

text = "hello there hi there"
dic = dict()
for w in text.split():
    if w in dic.keys():
        dic[w] = dic[w]+1
    else:
        dic[w] = 1

gives

>>> dic
{'hi': 1, 'there': 2, 'hello': 1}
kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • 2
    I would suggest `dic[w] = dic.get(w, 0) + 1` instead of the `if else` – jamylak May 15 '13 at 07:00
  • 1
    1. you could use `{}` instead of `dict()` 2. `if w in dic:` works as is without `dic.keys()` 3. `dic[w] += 1` instead of `dic[w] = dic[w] + 1`. If you don't want to use Counter; you could use `dic = collections.defaultdict(int)` and remove `if/else`. Here's [a performance comparison](http://stackoverflow.com/q/2522152/4279) – jfs May 15 '13 at 11:13