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

标签 python list function dictionary parameters

我有一个包含字典的列表。 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/

相关文章:

python - 在 Python 中实现实时信号处理——如何连续捕获音频?

python - “较低”对象没有属性 'partition'

java - 在 O(n) 时间内查找排序链表中的重复项

python - 如何使用函数将列表相乘?

c++ - 在复制构造函数双向链表中没有得到头节点和尾节点的正确地址

c - 我声明的结构中缺少什么

python - Pycrypto AES-CTR 实现

python - Pandas /matplotlib : change tick label format (force plain not scientific)

python - 从数组中获取第一个前一个不匹配的元素

r - 使用 R 优化/矢量化数据库查询