2

I am learning Python and I have a dictionary like this:

category = { "World news": [0], "Politics": [0], ... }

where now I am reading articles from a file. Every article has a category. So I would like the 0 I have to every key to be incremented by 1 when a category is meet and to augment the array every key has with the article it found. So the output would be:

category = {  "World news": [2, "foo bla content of", "content of 2nd article"],
              "Politics": [1, "only 1 article here"],
              ...
}

Here is what I have now:

for row in iter_reader:
    category[row[4]][0] = category[row[4]][0] + 1
    category[row[4]].append(category[row[3]])

but I think it interprets an article as a key and throws a relevant error:

File "./wordCloud.py", line 41, in category[row[4]].append(category[row[3]]) KeyError: 'When Apple unveiled its in January 2010..

Any ideas please?


row[4] is the category of the article. row[3] is the content of the article.

As Bryan Oakley said, I do not need to keep a counter, so I am now thinking in this direction...

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Why do you need to keep a counter? You can get the number of articles using the `len` function. – Bryan Oakley Jan 19 '16 at 14:31
  • Oh didn't know about that @BryanOakley. That makes sense, an example would be nice.. I am trying to do what you said now. – gsamaras Jan 19 '16 at 14:32
  • 2
    It appears you're simply misplacing some indices, but without seeing `row` it is hard to tell. Also, stackoverflow is not a debugger. – Joost Jan 19 '16 at 14:32
  • @Joost `row` indices are fine. Of course it's not, that's why I am asking about it here. You see the problem is not the indices, the problem is that I haven't understand something good in Python. :) I am updating. – gsamaras Jan 19 '16 at 14:33
  • Ah, adding that error does help. I'll post an answer instead, I guess. – Joost Jan 19 '16 at 14:35
  • You are doing fine by criticizing @Joost, many people use SO as dbg, thanks! – gsamaras Jan 19 '16 at 14:36

2 Answers2

2

In the third line, you are appending category[row[3]] where you should probably be appending simply row[3]. You are currently using row[3] as an index for the category list, but I suspect row[3] is actually the content you want to append.

Joost
  • 4,094
  • 3
  • 27
  • 58
2

Assuming that you have each article stored in a dictionary, where the key is the content and the value is the category. You could use something like the following piece of code:

categories = {"news"     : [0],
              "politics" : [0]}

articles = {"content1" : "news",
            "content2" : "politics"}

for key, value in articles.items():
    for keyDic, valDict in categories.items():
        if (value == keyDic):
            valDict[0] += 1
            valDict.append(key)
            continue
giograno
  • 1,749
  • 3
  • 18
  • 30
  • Grazie Giovanni, but that's not the case, since I want every category to have all the corresponding contents. What does `articles.items()` gives you? I am *new* to Python. +1 though for a new approach. – gsamaras Jan 19 '16 at 20:40
  • 1
    The method `items()` return a list of dictionary tuple pairs (key, value). In my example, the `category` dictionary has the tag string as key, and a list as value, where the element at position [0] is the counter, and the following ones are the corresponding content. For example, if you print the resulting `category` dictionary you will have the following output: `{"news" : [1, "content1"], "politics", [1, content2] } that is your desidered output. I used the `articles` dictionary to store your input because I does not know your file structure. – giograno Jan 20 '16 at 08:11