Python Forum

Full Version: print 3 return values from function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
just print it without formatting to see what the function returns.
Format expects 3 values, so unpack your tuple with *:

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

that worked!
Wow! I have learned one more thing  Shy