找到你要的答案

Q:Finding averages using two dictionaries

Q:使用两个字典查找平均值

I have two dictionaries that I wish to use:

dictionary1 = {'Reece': 2, 'Harry': 3, 'Jack': 3, 'Sam': 3, 'Dom': 3}
dictionary2 = {'Reece': 1, 'Harry': 14, 'Jack': 8, 'Sam': 16, 'Dom': 5}

I wish to divide each value in dictionary2 by the value in dictionary1, and print this out. This would print something like:

Reece:0.5

Harry:4.66

I'm currently trying to print and round these by using this code, which is erroring:

for b in dictionary2
            print('{}: {} '.format(b[0], round(b[1]/dictionary1[1], 2)))

我有两本字典想用:

dictionary1 = {'Reece': 2, 'Harry': 3, 'Jack': 3, 'Sam': 3, 'Dom': 3}
dictionary2 = {'Reece': 1, 'Harry': 14, 'Jack': 8, 'Sam': 16, 'Dom': 5}

I wish to divide each value in dictionary2 by the value in dictionary1, and print this out. This would print something like:

Reece:0.5

Harry:4.66

我正在尝试打印和圆这些通过使用此代码,则为:

for b in dictionary2
            print('{}: {} '.format(b[0], round(b[1]/dictionary1[1], 2)))
answer1: 回答1:

Here's one way to do it using keys

In [74]: {k: round(float(dictionary2[k])/dictionary1[k],2) for k in dictionary2}
Out[74]: {'Dom': 1.67, 'Harry': 4.67, 'Jack': 2.67, 'Reece': 0.5, 'Sam': 5.33}

Or use items as @JuniorCompressor pointed out

In [75]: {k: round(float(v)/dictionary1[k], 2) for k, v in dictionary2.items()} 
Out[75]: {'Dom': 1.67, 'Harry': 4.67, 'Jack': 2.67, 'Reece': 0.5, 'Sam': 5.33}

这里有一个方法来使用按键

In [74]: {k: round(float(dictionary2[k])/dictionary1[k],2) for k in dictionary2}
Out[74]: {'Dom': 1.67, 'Harry': 4.67, 'Jack': 2.67, 'Reece': 0.5, 'Sam': 5.33}

或使用道具“JuniorCompressor指出

In [75]: {k: round(float(v)/dictionary1[k], 2) for k, v in dictionary2.items()} 
Out[75]: {'Dom': 1.67, 'Harry': 4.67, 'Jack': 2.67, 'Reece': 0.5, 'Sam': 5.33}
answer2: 回答2:
results = {item:round(dictionary2.get(item)/dictionary1.get(item), 2) for item in dictionary1}

results:

{'Reece': 0.5, 'Harry': 4.67, 'Sam': 5.33, 'Dom': 1.67, 'Jack': 2.67}

print(*(': '.join([item, str(results.get(item))]) for item in results), sep='\n')

Reece: 0.5

Harry: 4.67

Sam: 5.33

Dom: 1.67

Jack: 2.67

results = {item:round(dictionary2.get(item)/dictionary1.get(item), 2) for item in dictionary1}

结果:

{ 'reece”:0.5,“哈利”:4.67,“山姆”:5.33、“DOM”:1.67,“杰克”:2.67 }

print(*(': '.join([item, str(results.get(item))]) for item in results), sep='\n')

Reece:0.5

Harry:4.67

山姆:5.33

DOM:1.67

杰克:2.67

answer3: 回答3:

hovewer the logic is almost same with solutions above, you can intuitively catch what is happening. you can format floats for your need when printing or storing

>>> for k1 in dictionary2.keys():
...     for k2 in dictionary1.keys():
...             if k1 == k2:
...                     print k1, float(dictionary2[k1])/dictionary1[k2]

the below one is more simple - so python interpeteter will not overloaded

>>> for k in dictionary2.keys():
...     print k, float(dictionary2[k])/dictionary1[k]

hovewer的逻辑与解决方案几乎相同,你可以直观地捕捉正在发生的事情。您可以在打印或存储时为您的需要格式化浮动

>>> for k1 in dictionary2.keys():
...     for k2 in dictionary1.keys():
...             if k1 == k2:
...                     print k1, float(dictionary2[k1])/dictionary1[k2]

下面的一个更为简单的所以Python interpeteter不会超载

>>> for k in dictionary2.keys():
...     print k, float(dictionary2[k])/dictionary1[k]
python  python-3.x  dictionary