我有一个包含字典的列表。 mylist 的示例:
产品 = [{u'title': u'product_A', u'int': [{u'content': 8112, u'name': u'orbitnumber'}, {u'content' : 107, u'name': u'relativeorbitnumber'}], u'double': {u'content': 23.4161, u'name': u'cloudcoverpercentage'}]
如果我想查找特定项目,我会使用“for 循环”。 例如:
for i in range(len(products)):
unzip(products[i]["title"])
这段代码工作正常。 现在,我想将该列表作为参数传递给一个新函数。
如何定义可以将“产品”列表作为参数的函数?我知道在 python 中你可以使用 *args 传递一个列表作为参数。但是我如何在函数中以通用方式定义例如 products[list_index]["key"] 。
最佳答案
例如您的函数是 my_f 您可以按如下方式定义它:
def my_f(my_var):
# then you can do anything you want
my_var[list_index]["key"] = "0123"
# Do some other works
# ...
# You can return my_var if you need the above change
return my_var
在调用 my_f 函数并将“products”作为 my_f 函数的参数传递的其他地方
new_list = my_f(products)
传递参数只是一种情况。 请注意,在 python 中,您可以传递任何内容作为参数
关于python - 如何将字典列表作为函数参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45503670/