0

System: WIN10

IDE: MS VSCode

Language: Python version 3.7.3

Library: pandas version 1.0.1

Data source: base data supplied below

Dataset: base data supplied below

I am having an issue for some reason when trying to use the "map" function to map A converter function (I built using lambda) to iterate across a list of sample temperatures. The sample code is supplied below and it keeps throwing the following error: TypeError: () takes 0 positional arguments but 1 was given

Steps were taken:

  1. tested independent pieces of the code to ensure the list of tuples in temps made since
  2. searched online for the error code and could not find anything

Code:

temps = [('Berlin', 29), ('Cairo', 36), ('Buenos Aires', 19), ('Los Angeles', 26), ('Tokyo', 27), ('New York', 28), ('London', 22), ('Beijing', 32)]

c_to_f = lambda: (data[0], (9/5)*data[1] + 32)

list(map(c_to_f, temps))

error

TypeError: () takes 0 positional arguments but 1 was given
Alfred Hull
  • 139
  • 11

1 Answers1

1

The map function will pass each element of temps as an argument to c_to_f.

Change your c_to_f definition so it takes an argument:

def c_to_f(data):
    return data[0], (9/5)*data[1] + 32

or just do:

list(map(lambda data: (data[0], (9/5)*data[1] + 32), temps))
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Is there a way to do this using lambda, someone else was able to use the lambda map marriage the way I have it above and it worked for them. I am trying to find out if there is something I did wrong maybe? – Alfred Hull May 29 '20 at 16:09
  • 1
    Yes, but it's considered bad style to name a lambda function. The whole point of a lambda expression is for cases where you just need to pipe something together and don't need to name a function for it; if you need to name a function, just use `def`. :) Will also give an example with lambda. – Samwise May 29 '20 at 16:13
  • Thank you Samwise! – Alfred Hull May 29 '20 at 19:44