[英]Extract key1:value1 from dictionary 1 and key1:value1 from dictionary 2, assign them to 4 different variables, loop
What I need to do:我需要做什么:
Example:例子:
d_one = {1:z, 2:x, 3:y}
d_two = {9:o, 8:n, 7:m}
the result has to be结果必须是
a = 1
b = z
c = 9
d = o
(calling some other methods using those variables here) (moving on to the next iteration) (在此处使用这些变量调用其他一些方法)(继续下一次迭代)
a = 2
b = x
c = 8
d = n
(and so on) (等等)
My brain is overloaded on this one.我的大脑在这件事上超负荷了。 Since I can't nest for loops to accomplish this task, I guess the correct usage of 'and' statement should do it?
既然我不能嵌套for循环来完成这个任务,我猜'and'语句的正确用法应该可以做到吗? I have no idea how so I try to split it up...
我不知道怎么弄,所以我试着把它分开......
d_one = {'1':'z', '2':'x', '3':'y'}
d_two = {'9':'o', '8':'n', '7':'m'}
for i in range(0, len(d_one)):
for a in list(d_one.keys())[i]:
a = d_one.keys()[i]
b = d_one[a]
for c in list(d_two.keys())[i]:
c = d_two.keys()[i]
d = d_two[c]
print(a, b, c, d)
output: output:
TypeError: 'dict_keys' object is not subscriptable
Try this:尝试这个:
d_one = {'1':'z', '2':'x', '3':'y'}
d_two = {'9':'o', '8':'n', '7':'m'}
for (a,b), (c,d) in zip(d_one.items(), d_two.items()):
print(a, b, c, d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.