Question: merging two python dictionaries
0
gravatar for t86dan
7 months ago by
t86dan30
t86dan30 wrote:

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 • 215 views
ADD COMMENTlink modified 7 months ago by goodez470 • written 7 months ago by t86dan30

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 REPLYlink written 7 months ago by RamRS25k

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 REPLYlink written 7 months ago by RamRS25k
0
gravatar for goodez
7 months ago by
goodez470
United States
goodez470 wrote:

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 COMMENTlink modified 7 months ago • written 7 months ago by goodez470

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 REPLYlink modified 7 months ago • written 7 months ago by RamRS25k

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 REPLYlink written 7 months ago by goodez470

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 REPLYlink written 7 months ago by RamRS25k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 999 users visited in the last hour