Depending on a condition I need to get a value from one or another function. I'm trying to put it inside a simple If ... Else
statement. I tried to use %s
string formatting but it won't work. Below code, so it will become more clear what I try to do:
if condition:
item = my_list['%s']
else:
item = my_other_list['%s']
# now I do something with this value:
print item % 3
This way I tried to print
3rd value of one or other list if the condition
was True of False. This returned an error about list indices being string. So I tried to put it inside int()
what didn't help.
How should I do it? The problem is I get the value later than I declare what item
is.
EDIT I will add some more infos here:
I have a for
loop, that goes through ~1000 elements and processes them. If the condition
is True
, it calls one function or another if false. Now, I don't want to check the same condition 1000 times, because I know it won't change during the time and would like to check it once and apply the method to all of the elements.
More code:
if self.dlg.comboBox_3.currentIndex == 0:
item = QCustomTableWidgetItem(str(round((sum(values['%s'])/len(values['%s'])),2)))
else:
item = QCustomTableWidgetItem(str(round(sum(values['%s'],2))))
for row in range(len(groups)):
group = QTableWidgetItem(str(groups[row]))
qTable.setItem(row,0,group)
qTable.setItem(row,1,item % row)
This is the actual code. Not the '%s'
and '% row'. I used simplified before not to distract from the actual problem, but I think it's needed. I'm sorry if it wasn't a good decision.