[英]For each value in 1 dictionary take keys and for those keys in 2 dictionary print difference in values for all specific pairs of keys
[英]How to print values from dictionary in pairs opposite each other
例如,这段代码:
data = {"Name": "salat "
, "Ingredient 1": "salad ", "Number of in1": 60
, "Ingredient 2": "salt ", "Number of in2": 19
, "Ingredient 3": "oil ", "Number of in3": 20
, "Ingredient 4": "cheese ", "Number of in4": 50
, "Ingredient 5": "pepper", "Number of in5": 10
}
for key in data:
print(data[key])
印刷:
salat
salad
60
salt
19
oil
20
cheese
50
pepper
10
但我想要:
salat
salad 60
salt 19
oil 20
cheese 50
pepper 10
我怎样才能得到这个?
假设字典总是以这种方式排序,您可以检查项目是偶数还是奇数并更改print
结束值:
for i,v in enumerate(data.values()):
print(v, end=' ' if i%2 else '\n')
输出:
salat
salad 60
salt 19
oil 20
cheese 50
pepper 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.