Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Updated to reflect the matching on the values...</p> <p>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.</p> <pre><code>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] </code></pre> <p>I was not sure if you needed <code>clusterid2denoiseidlist</code> or not, so I added a new <code>known_clusters</code> to hold the mapping from values to lists.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload