2012-02-26 79 views
2

pyshp模块中的功能record()需要一个序列输入:Python函数期望一个元组,我拥有的是一个列表。我如何调用这个函数?

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

我所拥有的是一个列表:

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

当我打电话record()功能是这样的:

outfile.record(row) 

我收到一个tuple index out of range错误。原来函数接收

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

如何正确调用record? 我试过了

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

但是这也行不通。

+0

这不是它期望的顺序;这是几个论点。 “序列”是列表,元组,字符串等的通用术语......即可迭代的东西,但仍然是单一的特定对象。 'outfile.record'希望你能通过不止一件事。 – 2012-02-26 08:55:05

回答

10
outfile.record(*row) 

这将解压一个序列为单个参数。这是一个formal description of this syntax from the language reference,这是一个informal description from the tutorial

注有一个类似的构建体,其将解包的映射(字典)插入关键字参数:

functiontakingkeywordarguments(**mydict) 
+0

谢谢!我搜索了python文档,但找不到参考。我会在这里预期它:http://docs.python.org/library/stdtypes.html – mvexel 2012-02-26 01:38:34

相关问题