![](/img/trans.png)
[英]How can I use the append function to make a list of dictionaries in a list of dictionaries
[英]how can I pass a list of dictionaries as function parameter?
我有一個包含字典的列表。 我的清單示例:
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"])
這段代碼可以正常工作。 現在,我想將該列表作為參數傳遞給新函數。
如何定義可以將“產品”列表作為參數的函數? 我知道在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函數,並將“產品”作為my_f函數的參數傳遞
new_list = my_f(products)
傳遞參數只是一種情況。 請注意,在python中,您可以將任何內容作為參數傳遞
我同意Toandd。 另一種方法是:1.使用您要更改或包含在字典中的參數定義字典
dictionary = dict(key1=value1,key2=value2)
dictionary_treatment = your_function(dictionary)
這個對我有用!
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.