python3如何从lis传递字典

2024-10-02 12:38:41 发布

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

我在将字典从一个列表传递到另一个函数时遇到了问题。我很肯定是因为我的字典在一系列字典中。你知道吗

示例:

我想要的是:{"part": x2f5, "color": red, "amount": 10}

传递给函数时得到的结果:[{part": x2f5, "color": red, "amount": 10}]

if (len(order["inventory"]) >= 1):
    #I want to double check here if the part is already in the order 
    if new_order["part"] in (i["part"] for i in order["inventory"]):
        if already_in(new_order, order["inventory"]): 

如果我需要补充更多信息,请告诉我。你知道吗


Tags: the函数in列表newif字典order
2条回答

你可以试试这个:

def the_function(*s):
   pass

m = [{"part": x2f5, "color": red, "amount": 10}]
the_function(*m)

此解决方案将允许您将“n”个字典项从列表传递到the_function。你知道吗

谢谢大家!你知道吗

不过,我自己也能解决这个问题,如果有人好奇的话,我就是这么做的

for i in order["inventory"]:
   if (i["part"] == new_order["part"]):
      if (already_in(new_order, i)): 

相关问题 更多 >

    热门问题