Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:52:20+00:00 2026-06-15T09:52:20+00:00

Need help merging two dictionaries using the keys from one to look at values

  • 0

Need help merging two dictionaries using the keys from one to look at values in another. If returns true it would append its own values into the other dictionary (updating it.. but not overwriting already present values)

The code (sorry first custom script ever):

otuid2clusteridlist = dict()
finallist = otuid2clusteridlist
clusterid2denoiseidlist = dict()

#first block, also = finallist we append all other blocks into.
for line in open('cluster_97.ucm', 'r'):
    lineArray = re.split('\s+',line)
    otuid = lineArray[0]
    clusterid = lineArray[3]
    if otuid in otuid2clusteridlist:
        otuid2clusteridlist[otuid].append(clusterid)
    else:
        otuid2clusteridlist[otuid] = list()
        otuid2clusteridlist[otuid].append(clusterid)

#second block, higher tier needs to expand previous blocks hash
for line in open('denoise.ucm_test', 'r'):
    lineArray = re.split('\s+', line)
    clusterid = lineArray[4]
    denoiseid = lineArray[3]
    if clusterid in clusterid2denoiseidlist:
        clusterid2denoiseidlist[clusterid].append(denoiseid)
    else:
        clusterid2denoiseidlist[clusterid] = list()
        clusterid2denoiseidlist[clusterid].append(denoiseid)  

#print/return function for testing (will convert to write out later)
for key in finallist:
    print "OTU:", key, "has", len(finallist[key]), "sequence(s) which", "=", finallist[key]

Block one returns

OTU: 3 has 3 sequence(s) which = ['5PLAS.R2.h_35336', 'GG13_52054', 'GG13_798']
OTU: 5 has 1 sequence(s) which = ['DEX1.h_14175']
OTU: 4 has 1 sequence(s) which = ['PLAS.h_34150']
OTU: 7 has 1 sequence(s) which = ['DEX12.13.h_545']
OTU: 6 has 1 sequence(s) which = ['GG13_45705']

Block two returns

OTU: GG13_45705 has 4 sequence(s) which = ['GG13_45705', 'GG13_6312', 'GG13_32148', 'GG13_35246']

So the goal is to add block two’s out put into block one. I would like it to add in like this

...
 OTU: 6 has 4 sequence(s) which = ['GG13_45705', 'GG13_6312', 'GG13_32148', 'GG13_35246']

I attempted dic.update but it just adds block twos contents into block one since the key is not present in block one.

I think my issue is more complicated, I need block two to look within block one’s value for its key and append values into that list.

I have been trying for loops and .append (similar to the code already wrote) but I am lacking the overall knowledge of python to solve this.

Ideas?

Additions,

Some subsets of the data:

cluster_97.ucm (block one’s file):

5 376 * DEX1.h_14175 DEX1.h_14175
6 294 * GG13_45705 GG13_45705
0 447 98.7 DEX22.h_37221 DEX29.h_4583
1 367 98.9 DEX14.15.h_35477 DEX27.h_779
1 443 98.4 DEX27.h_3794 DEX27.h_779
0 478 97.9 DEX22.h_7519 DEX29.h_4583

denoise.ucm_test (block two’s file):

11 294 * GG13_45705 GG13_45705
11 278 99.6 GG13_6312 GG13_45705
11 285 99.6 GG13_32148 GG13_45705
11 275 99.6 GG13_35246 GG13_45705

I picked these subsets because the 2nd line in file one is what file two would would be updating.

If anyone wants to give it a shot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:52:21+00:00Added an answer on June 15, 2026 at 9:52 am

    Updated to reflect the matching on the values…

    I think the solution to your problem can be found in the fact that lists a mutable in Python and variables with mutable values are just references. So we can use a second dictionary mapping the value to the list.

    import re
    
    otuid2clusteridlist = dict()
    finallist = otuid2clusteridlist
    clusterid2denoiseidlist = dict()
    known_clusters = dict()
    
    #first block, also = finallist we append all other blocks into.
    for line in open('cluster_97.ucm', 'r'):
        lineArray = re.split('\s+',line)
        otuid = lineArray[0]
        clusterid = lineArray[3]
        if otuid in otuid2clusteridlist:
            otuid2clusteridlist[otuid].append(clusterid)
        else:
            otuid2clusteridlist[otuid] = list()
            otuid2clusteridlist[otuid].append(clusterid)
    
        # remeber the clusters
        known_clusters[clusterid] = otuid2clusteridlist[otuid]
    
    #second block, higher tier needs to expand previous blocks hash
    for line in open('denoise.ucm_test', 'r'):
        lineArray = re.split('\s+', line)
        clusterid = lineArray[4]
        denoiseid = lineArray[3]
        if clusterid in clusterid2denoiseidlist:
            clusterid2denoiseidlist[clusterid].append(denoiseid)
        else:
            clusterid2denoiseidlist[clusterid] = list()
            clusterid2denoiseidlist[clusterid].append(denoiseid)
    
        # match the cluster and update as needed
        matched_cluster = known_clusters.setdefault(clusterid, [])
        if denoiseid not in matched_cluster:
            matched_cluster.append(denoiseid)
    
    
    
    #print/return function for testing (will convert to write out later)
    for key in finallist:
        print "OTU:", key, "has", len(finallist[key]), "sequence(s) which", "=", finallist[key]
    

    I was not sure if you needed clusterid2denoiseidlist or not, so I added a new known_clusters to hold the mapping from values to lists.

    I’m not sure I covered all the edge cases in your real problem, but this generates the desired output given the supplied test inputs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help with deleting call log from multi numbers, using the following code i
Need Help to Remove this Part .php?number= from Link with / using .htaccess Old
Sorry, guys.I am quite new in mysql but I do need help from getting
Need help with implementing a pure abstract class via inheritance , using namespace to
Need help with PHP/MySql. Need to select all the records from 'today'. My table
Need help to convert code from asp control to input type to fetch file
I need some help. I googled for solution, but I didn't found one. I
Need help transfer sql to sequel: SQL: SELECT table_t.curr_id FROM table_t INNER JOIN table_c
I have two tables on different servers, and I'd like some help finding an
I have created branches for one project, but now I have a need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.