Leockl
Leockl

Reputation: 2156

How to iterate to create variables in a list

Suppose I have the following code:

classifiers_name_all = [('AdaBoostClassifier', AdaBoostClassifier(), 'AdaBoost'),
                        ('BernoulliNB', BernoulliNB(), 'Bernoulli Naive Bayes'),
                        ('DummyClassifier', DummyClassifier(), 'Dummy Classifier')]

clf_values = []
for clf_na in classifiers_name_all:
    clf_values.append((locals()['score_'+clf_na[0]+'_mean'], locals()['score_'+clf_na[0]+'_stddev']))
clf_values

The code above doesn't quite work.

I want to get a list which contains the variables:

clf_values = [(score_AdaBoostClassifier_mean, score_AdaBoostClassifier_stddev),
              (score_BernoulliNB_mean, score_BernoulliNB_stddev)        
              (score_DummyClassifier_mean, score_DummyClassifier_stddev)]

How do I do this? Many thanks.

Upvotes: 0

Views: 94

Answers (1)

Akash Sundaresh
Akash Sundaresh

Reputation: 106

From whatever info you have given so far, I infer that there are no key errors and the resultant list is a list containing nones.

This can only mean, that your code works fine but the variables u are trying to access have 'None' values assigned to them. Check why your values are having None values and once that is fixed, this list will get desired values.

Upvotes: 1

Related Questions