1
\$\begingroup\$

I'm wondering if there is a faster way to do the following code, which works fine. It needs to be applied to every row of the DataFrame df. class_dct contains a different NLTK NaiveBayes classifier for each of 9 regions.

for region in regions:
    new_app=[]
    for title in df.booktitle:
        nlp_genre=class_dct['Classif_%s'% region].classify(features(title))
        new_app.append((title,region,nlp_genre))
\$\endgroup\$
1
\$\begingroup\$

I guess the Python interpreter will optimise most or all of this on its own, but you could try a list comprehension and removing an intermediate variable:

new_app = [(title, region, class_dct['Classif_%s'% region].classify(features(title))) for title in df.booktitle]
|improve this answer|||||
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.