mardi 14 juillet 2020

How do you check that each entry in a dictionary is not empty in python?

When trying to iterate over a dictionary, I tried to make sure that each value in the dictionary is not None or empty and an error is thrown. The code is bellow.

articleSummaries = {}
for techUrlDictionary in [newYorkTimesTechArticles, washingtonPostTechArticles]:
    for articleUrl in techUrlDictionary:
        if techUrlDictionary[articleUrl][0] is not None:
            if len(techUrlDictionary[articleUrl][0]) > 0:
                fs = FrequencySummarizer()
                summary = fs.extractFeatures(techUrlDictionary[articleUrl],25)
                articleSummaries[articleUrl] = {'feature-vector': summary,
                                               'label': 'Tech'}

And the error message looks like this.

----> 5         if techUrlDictionary[articleUrl][0] is not None:
      6             if len(techUrlDictionary[articleUrl][0]) > 0:
      7                 fs = FrequencySummarizer()

TypeError: tuple indices must be integers or slices, not dict

What would be the best way to execute these checks? So this error is avoided?

Aucun commentaire:

Enregistrer un commentaire