简体   繁体   English

从字典 1 中提取 key1:value1 和从字典 2 中提取 key1:value1,将它们分配给 4 个不同的变量,循环

[英]Extract key1:value1 from dictionary 1 and key1:value1 from dictionary 2, assign them to 4 different variables, loop

What I need to do:我需要做什么:

  • for each key in dictionary1 extract key1:value1 from dictionary1 and key1:value1 from dictionary2对于dictionary1 中的每个键,从dictionary1 中提取key1:value1 和从dictionary2 中提取key1:value1
  • assign those 2 pairs to 4 different variables将这 2 对分配给 4 个不同的变量
  • use those variables in other methods在其他方法中使用这些变量
  • move on to the next iteration (extract key2:value2 of both dictionaries 1 and 2, and assign to the same 4 variables)继续下一次迭代(提取字典 1 和 2 的 key2:value2,并分配给相同的 4 个变量)

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.

相关问题 从 {key1: [value1, value2...]} 到 {value1: [key1, key2...]} 的有效方法 - Efficient way to get from {key1: [value1, value2...]} to {value1: [key1, key2...]} 如何将字典的格式更改为value1; key1; 在Python 3.6中? - How do I change the format of a dictionary to value1;key1; in Python 3.6? 将两个 JSON {key1:value1 , key2:value2} 组合成单个键(即 {key3:value1,value2})Python - combining two JSON {key1:value1 , key2:value2} to single key (i e {key3:value1,value2}) Python 使用元组键从Dictionary [key1,key2]获取第一个键的列表 - Get list of first keys from Dictionary[key1,key2] with tuple keys 根据元组键(键1,键2)中的第一个键过滤字典,其中键1 - Filtering dictionary based on first key in a tuple key (key1, key2), where key1 如何从python中key1的最大值列表中找到key2的最小值的对象 - how to find object of min value of key2 from list of max values of key1 in python 将最后一个等式标记后的字符串与嵌套字典中的key1匹配 - Matching the string after the last equation mark to key1 in a nested dictionary 从字符串中提取字典键值 - extract a dictionary key value from a string 使用作为列表的键从字典中提取值 - Extract value from a dictionary using key that is a list 如何在 python 中迭代地将字典的键和值分配给不同的变量 - how to assign key and value of dictionary to different variables iteratively in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM