Python Forum
print 3 return values from function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print 3 return values from function
#1
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
Reply
#2
just print it without formatting to see what the function returns.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Format expects 3 values, so unpack your tuple with *:

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

that worked!
Reply
#6
Wow! I have learned one more thing  Shy
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Need to return 2 values from 1 DF that equals another DF cubangt 5 593 Oct-21-2023, 02:45 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 562 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,174 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Print names in x-axis of a time-series values hobbyist 4 1,178 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,131 Feb-04-2023, 12:30 PM
Last Post: caslor
  Adding values with reduce() function from the list of tuples kinimod 10 2,514 Jan-24-2023, 08:22 AM
Last Post: perfringo
  How to print variables in function? samuelbachorik 3 851 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  [Solved]Return values from npyscreen Extra 2 1,093 Oct-09-2022, 07:19 PM
Last Post: Extra
  How to print the output of a defined function bshoushtarian 4 1,237 Sep-08-2022, 01:44 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020