For example,
a = {'apple': 12, 'banana': 20}
b = {'Apple': 3, 'Banana': 6}
Divide these two dictionaries into two
{'Apple': 4, 'Banana': 3.33333}
Isn't there a way to make it come out like this?
dictionary python
Isn't there a way to make it~~?ㅜ
No, you have to make it up.
I think you'll want to cry more if I say this, but there's nothing to be afraid of. All programming problems are eventually the task of receiving input, processing it into a set rule, and providing output (if necessary). If so, this is also a problem that only needs to be sure of input, rules, and output.
Let's think about the input, the rules, the output of this problem.
Write down these inputs, rules, and outputs in code.
def get_division_of_matching_values(dicA, dicB) :
# The Third Dictionary
dicC = {}
# Do something to the third dictionary according to the rules.
for k in dicA :
If dicB has a k key:
v = divide the value of k in dicA by the value of k in dicB
Assigning v to the k value of dicC
# Print it out.
return dicC
It's a matter of understanding this calmly and doing it. You don't have to cry.
I'm attaching it by referring to how to use the Pandas Series.
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> a = {'Apple': 12, 'Banana': 20}
>>> b = {'Apple': 3, 'Banana': 6}
>>> import pandas as pd
>>> pd.Series(a)
Apple 12
Banana 20
dtype: int64
>>> s_a = pd.Series(a)
>>> s_b = pd.Series(b)
>>> s_a
Apple 12
Banana 20
dtype: int64
>>> s_b
Apple 3
Banana 6
dtype: int64
>>> s_a/s_b
Apple 4.000000
Banana 3.33333
dtype: float64
>>> s_c = s_a/s_b
>>> s_c
Apple 4.000000
Banana 3.33333
dtype: float64
>>> s_c.to_dict()
{'Apple': 4.0, 'Banana': 3.33333333333335}
© 2024 OneMinuteCode. All rights reserved.