merging two python dictionaries
1
0
Entering edit mode
5.4 years ago
t86dan ▴ 30

I have two dictionaries with same keys but different values and I want to concatenate the values into a single dictionary like this:

INPUT

dict_1 = {0: 0.001469768840129216, 1: 0.004734336468720571, 2: -0.005826401939721929, 3: -0.02846783060538032, 4: 0.008589455025104595, 5: -0.0005503362650310596}

dict_2 = {0: 0.355343768840129216, 1: 0.678679878468720571, 2: -0.045776401939778954, 3: -0.67476463060538032, 4: 0.646746674675123533, 5: -0.3562346762650313643}

I want the output to look something like this

OUTPUT

dict_3 = {0: array([0.001469768840129216, 0.355343768840129216]), 1: array([0.004734336468720571, 0.678679878468720571]), 2: array([-0.005826401939721929,  -0.045776401939778954]), 3: array([-0.02846783060538032, -0.67476463060538032]), 4: array([0.008589455025104595,  0.646746674675123533]), 5: array([-0.0005503362650310596,  -0.3562346762650313643])}
software error • 997 views
ADD COMMENT
0
Entering edit mode

Hi,

Please edit your post and explain how this is related to bioinformatics and what you've tried by yourself to solve the problem. If it is not related to bioinformatics, please search/ask Stack Overflow.

ADD REPLY
0
Entering edit mode

Please use the formatting bar (especially the code option) to present your post better. You can use backticks for inline code (`text` becomes text), or select a chunk of text and use the highlighted button to format it as a code block. I've done it for you this time.
code_formatting

ADD REPLY
0
Entering edit mode
5.4 years ago
goodez ▴ 640

Since they have the same keys, this would work:

dict_3={}
for i in dict_1:
    dict_3[i]=[dict_1[i], dict_2[i]]
ADD COMMENT
0
Entering edit mode

If it's just a loop you need, why not

d3 = [ d1[i], d2[i] ] for i in 1:d1.length() #not sure if dict.length() is a thing in python

or something like that? I'm pretty sure there are more functions that could do this, but comprehensions are more python-y than loops, no?

ADD REPLY
0
Entering edit mode

I use loops all the time in python. I just find comprehensions harder to read personally. In any case, I know mine works.

As an aside, when I start worrying about being "pythonic", I don't even want to code anymore. Like why can't I just write something that works and is clear?

ADD REPLY
0
Entering edit mode

Whatever works for you, my friend. Programming languages give you the freedom to implement problem solving logic the way you see fit, so there is no "correct answer" as such. My thought process is: given that OP wants to use Python exclusively, why not go all in on how Python-y the solution is?

I am not a Python programmer, but I don't think comprehensions are challenging to read (to a certain limit). There are people that write comprehensions so crazy that the line starts looking like Perl, but the example above is fairly basic, I think.

ADD REPLY

Login before adding your answer.

Traffic: 1599 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6