Sahil Dahiya
Sahil Dahiya

Reputation: 721

how to loop over below statement

sorted(X_train_num.F12.unique())==sorted(X_test_num.F12.unique())

basically I want to run loop over different variable like F12,F11,F78 etc. so that I can avoid writing the single command again and again for different variable.

Does someone knows simple way to loop over this statement with different var(F12,F11, etc)

Upvotes: 1

Views: 63

Answers (2)

stefan.schroedl
stefan.schroedl

Reputation: 866

Side note: you can also do the comparison more efficiently by using set equality:

set(getattr(X_train_num, var)) == set(getattr(X_test_num, var))

Upvotes: 1

John1024
John1024

Reputation: 113864

Use getattr:

var = 'F12'
sorted(getattr(X_train_num, var).unique())==sorted(getattr(X_test_num, var).unique())

The above can be easily put in a loop. For example:

for var in ('F10', 'F11', 'F12'):
    sorted(getattr(X_train_num, var).unique())==sorted(getattr(X_test_num, var).unique())

Upvotes: 4

Related Questions