indepgroupl = ['a', 'b']
depgroupl = ["x", "y"]
for i in indepgroupl:
for j in depgroupl:
if i == 'categorical':
if i == 'numerical':
if j == 'numerical':
print("utter_indepnumcat_depnum")
elif j == 'categorical':
print("utter_indepnumcat_depcat")
elif j == 'time to event':
print("utter_indepnumcat_deptimetoevent")
if j == 'numerical':
print("utter_indep2cat_depnum")
elif i == 'factors':
if j == 'count':
print("utter_indepfac_depcount")
elif j == 'numerical':
print("utter_indepfac_depnum")
Is there a more elegant way to write this if/elif/else loop? Can I use list comprehension for this?
CodePudding user response:
You can store the conditional value and the string to print in a tupled list
for cat, val in [('numerical', 'depnum'), ('categorical', 'depcat'), ('time to event', 'deptimetoevent')]:
if i == cat:
print ('utter_indepnumcat_' val)
For just three entries you don't save much lines of code; it makes more sense if you have more entries.
CodePudding user response:
You can look into using a switch-like syntax. If you are using python v3.10 you can utilize the match and case syntax as detailed here.