简体   繁体   中英

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

I cant seem to figure out what the problem is here.i'm using this code to create multiple groups of numbers from an array.i'm not sure if it is a python compatibility issue or not.


data=[]
data=1,2,3,4,6,7,8,11,12
for k, g in groupby(enumerate(data), lambda i, x: i-x):
    print map(itemgetter(1), g)

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

Assuming this is Python 2.x - you're just missing a pair of brackets around the lambda 's parameters:

for k, g in groupby(enumerate(data), lambda (i, x): i-x):
    print map(itemgetter(1), g)

You here defined with lambda i, x : i - x a function that takes two parameters, but the groupby function only takes one parameter. In this case a 2-tuple.

You thus can obtain the elements with subscripting :

for k, g in groupby(enumerate(data), ):
    print(map(itemgetter(1), g))

or in , you can use iterable unpacking :

for k, g in groupby(enumerate(data), lambda : i - x):
    print(map(itemgetter(1), g))

here we thus unpack the tuple in two parameters i and x .

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