如何将字典列表作为函数参数传递?

2024-10-01 04:44:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份单子,里面有一本词典。 mylist示例:

products = [{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"])

这段代码工作得很好。 现在,我想把这个列表作为参数传递给一个fresh函数。在

我如何定义那个可以以“products”列表为参数的函数?我知道在python中,您可以使用:*args作为参数传递一个列表。但是,如何在函数中以一般方式定义products[list_index][“key”]。在


Tags: 函数name示例列表for定义titlecontent
2条回答

我同意今天的说法。 另一种方法是: 1使用要更改或包含在词典中的参数定义词典

dictionary = dict(key1=value1,key2=value2)
  1. 把字典传给你的部门

dictionary_treatment = your_function(dictionary)

对我有用!在

问候

你的函数是我的 您可以定义如下:

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 function并将“products”作为my_f function的参数传递

^{pr2}$

传递参数只是一种情况。 请注意,在python中,可以将任何内容作为参数传递

相关问题 更多 >