我正在试图弄清楚如何执行以下操作:
typedef struct node {
book data;
struct node *next;
} Node;
typedef Node* NodePtr;
这样
firstDictionary = {"firstKey" : "A", "secondKey" : "B"}
secondDictionary = {"firstKey" : 3, "secondKey" : 47}
emptyDictionary = {}
for key, value in firstDictionary, secondDictionary:
# emptyDictionary = some sort of append method...
答案 0 :(得分:1)
这很直截了当。您需要做的就是迭代其中一个字典项,并基本上使用第一个字典中的键找到第二个字典的关联值。
>>> firstDictionary = {"firstKey" : "A", "secondKey" : "B"}
>>> secondDictionary = {"firstKey" : 3, "secondKey" : 47}
>>> emptyDictionary = {value : secondDictionary.get(key, None) for key, value in firstDictionary.items()}
>>> print emptyDictionary
{'A': 3, 'B': 47}
如果您确定两个词典都具有相同的键,则将dict.get构造替换为字典索引
>>> {value : secondDictionary[key] for key, value in firstDictionary.items()}
{'A': 3, 'B': 47}