简体   繁体   中英

TypeError: <lambda>() missing 1 required positional argument

I am working with a json file and I need to create an array with which I can work later on in my Python code.

It needs to look like this [[1*5,1.75],[2*5,1.74],[3*5,1.76]...] so i can calculate l ike avgPoints[x][0]+avgPoints[x+1][0]

My current code looks like this:

data = json.loads(json_file)

avgData = data['diameter_measurement'].split(',')
avgStep = float(data['length']) / (len(avgData) - 1)
avgPoints = list(map(lambda index, delta: [index * avgStep, 1.75 + float(delta) / 1000.0], enumerate(avgData)))

But when I run my code it gives me following error:

->TypeError: <lambda>() missing 1 required positional argument: 'delta'

When you iterate over enumerate , you get back one item: the tuple (index, element) . This is not implicitly unpacked into index, delta when you call the lambda , so you have to unpack it yourself with something like lambda index_delta: "use index_delta[0] and index_delta[1]" . Alternatively you could define a function and unpack the function's single argument into index and delta .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM