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...