简体   繁体   English

将两个列表转换成字典?

[英]Convert two lists to a dictionary?

def dbupdate(self):
    dicDccp = dict(zip(self.dccpdate, self.dccpValue))
    dicDcep = dict(zip(self.dcepdate, self.dcepValue))
    dicDccp = sorted(dicDccp.keys())
    dicDcep = sorted(dicDcep.keys())
    print(type(dicDccp))
    print(type(dicDcep))
    for (k1, v1), (k2, v2) in zip(dicDccp.items(), dicDcep.items()):
        self.Db.dbupdate(self.symbol, self.threshold, k1, v1, k2, v2)

I tried to convert the two lists to a dictionary. 我试图将两个列表转换成字典。 But the types of dicDccp and dicDcep were still list s. 但是dicDccpdicDcep的类型仍然是list

在此处输入图片说明

The problem is the line dictDccp = sorted(dictDccp.keys()) 问题是dictDccp = sorted(dictDccp.keys())

sorted is a function that takes an iterable object (such as the keys() object that you give it) and returns a list. sorted是一个函数,它接受一个可迭代的对象(例如,您为其提供的keys()对象)并返回一个列表。 You are overwriting your dictDccp with this list. 您正在用此列表覆盖dictDccp。

If you were trying to iterate through the keys of the dictionaries in sorted order you could do 如果您尝试按排序顺序遍历字典的键,则可以执行

for (k1, v1), (k2, v2) in zip(sorted(dictDccp.items()), sorted(dictDcep.items())):
    self.Db.dbupdate(self.symbol, self.threshold, k1, v1, k2, v2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM