mve*_*xel 2 python tuples list
该函数record()中pyshp模块预计的序列作为输入:
outfile.record('First','Second','Third')
Run Code Online (Sandbox Code Playgroud)
我有一个清单:
row = ['First','Second','Third']
Run Code Online (Sandbox Code Playgroud)
当我这样调用record()函数时:
outfile.record(row)
Run Code Online (Sandbox Code Playgroud)
我收到一个tuple index out of range错误.事实证明功能接收
(['First','Second','Third'],)
Run Code Online (Sandbox Code Playgroud)
我怎么称呼record正确?我试过了
outfile.record((row[i] for i in range(len(row)))
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.
Fra*_*ila 11
outfile.record(*row)
Run Code Online (Sandbox Code Playgroud)
这会将序列解压缩到单个参数中.这是语言参考中对此语法的正式描述,这是本教程中的非正式描述.
请注意,有一个类似的构造,它将映射(dict)解压缩为关键字参数:
functiontakingkeywordarguments(**mydict)
Run Code Online (Sandbox Code Playgroud)
outfile.record(*row)
Run Code Online (Sandbox Code Playgroud)
*在这种情况下意味着"解包".它会将列表解压缩为一系列参数.
http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists