简体   繁体   中英

Python function expects a tuple, what I have is a list. How do I call this function?

The function record() in the pyshp module expects a sequence as input:

outfile.record('First','Second','Third')

What I have is a list:

row = ['First','Second','Third']

When I call the record() function like this:

outfile.record(row)

I get a tuple index out of range error. It turns out the function receives

(['First','Second','Third'],)

How do I call record correctly? I have tried

outfile.record((row[i] for i in range(len(row)))

but that doesn't work either.

outfile.record(*row)

This will unpack a sequence into individual arguments. This is a formal description of this syntax from the language reference , and this is an informal description from the tutorial .

Note there is a similar construct which will unpack a mapping (dict) into keyword arguments:

functiontakingkeywordarguments(**mydict)
outfile.record(*row)

The * in this case means "unpack." It will unpack a list into a series of arguments.

http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists

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