Python Forum
print 3 return values from function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: print 3 return values from function (/thread-2295.html)



print 3 return values from function - bluefrog - Mar-05-2017

Hi

I am attempting to print the values returned from the function, defined as follows:
def calc_accuracy(p_RDD, model):
  correct = p_RDD.map( lambda lp: 1 if model.predict(lp.features) == lp.label else 0).sum()
  count = p_RDD.count()
  accuracy = correct/count
  return correct, count, accuracy
but I get the following error:
---> 27   print('train set correct: {}, of total: {}, accuracy: {}'.format(calc_accuracy(train_RDD, model)) )

IndexError: tuple index out of range
Can anybody suggest a way I can call the function within a print ?
Thanks


RE: print 3 return values from function - wavic - Mar-05-2017

just print it without formatting to see what the function returns.


RE: print 3 return values from function - zivoni - Mar-05-2017

Format expects 3 values, so unpack your tuple with *:

print('train set correct: {}, of total: {}, accuracy: {}'.format( *calc_accuracy(train_RDD, model)) )
Q


RE: print 3 return values from function - buran - Mar-05-2017

star-unpack the tuple returned by your function
print('train set correct: {}, of total: {}, accuracy: {}'.format(*calc_accuracy(train_RDD, model)))



RE: print 3 return values from function - bluefrog - Mar-05-2017

thanks, zivoni, buran,

that worked!


RE: print 3 return values from function - wavic - Mar-05-2017

Wow! I have learned one more thing  Shy