从列表创建密钥对并附加到列表列表

Creating key pairs from lists and appending to a list of lists

我正在尝试从列表中创建密钥对,然后将它们附加到列表列表中。但是,它 return 是一个空列表。

  Combined = {}
  Awarded = []

  Keys = Contract_Keys + Details_Keys
  Values = Contract_Values + Details_Values
  #Create key pairs with multiple lists
  Combined = dict(zip(Keys, Values))

  #Append to the Awarded table
  Awarded.append(Combined)

  Final = pd.DataFrame(Awarded)

注意:我已将代码更改为上面的代码,以便我实际上可以 return 一个非空集。但是我现在遇到的问题是,当我压缩并听写键和值时,结果很奇怪:

Combined = dict(zip([a, b], [1,2])) 被 returned 为 [a1:1, b2:2) 而它应该是 [a :1, b:2]。 Value 与 Key 字段中的 Key 连接的任何原因?

不确定这是否是您想要的,但我能够通过列表理解来做到这一点?

Combined = {}
Awarded = []

#Created some test dictionaries
dictOne = {"k1" : "v1","k2" : "v2","k3" : "v3","k4" : "v4" }
dictTwo = {"k5" : "v5","k6" : "v6","k7" : "v7","k8" : "v8" }

#Got the keys
Contract_Keys = [keys for keys in dictOne]
Details_Keys = [keys for keys in dictTwo]

#Got the values
Contract_Values = [dictOne[values] for values in dictOne]
Details_Values = [dictTwo[values] for values in dictTwo]

########output###########
#['k1', 'k2', 'k3', 'k4'] 
#['k5', 'k6', 'k7', 'k8'] 

#['v1', 'v2', 'v3', 'v4'] 
#['v5', 'v6', 'v7', 'v8'] 

#Concat lists
Keys = Contract_Keys + Details_Keys
Values = Contract_Values + Details_Values

#Combined lists into single dictionary
Combined = dict(zip(Keys, Values))

#Combined kvp from dict into a list of lists
Awarded = [[key, Combined[key]] for key in Combined]

#########output#########
#[['k1', 'v1'], ['k2', 'v2'], ['k3', 'v3'], ['k4', 'v4'], ['k5', 'v5'], 
#['k6', 'v6'], ['k7', 'v7'], ['k8', 'v8']]

#{'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4', 'k5': 'v5', 'k6': 'v6', 
#'k7': 'v7', 'k8': 'v8'}