Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd change your <code>OutputNameDict</code> keys to be regular expression patterns, as follows:</p> <pre><code>OutputNameDict = {'h18_84pp_3A_MVP.*FIX': 1, 'p18_84pp_2B_MVP.*FIX': 2, 'g18_84pp_2A_MVP.*MIX': 0} </code></pre> <p>Then, using the <code>re</code> regular expression module, use that to match against the keys in <code>mydict</code>, and place the dictionary element into the appropriate key in <code>output_dicts</code> dictionary, as follows</p> <pre><code>import collections import re output_dicts = collections.defaultdict(dict) for k, v in mydict.iteritems(): for pattern, suffix in OutputNameDict.iteritems(): if re.match(pattern,k): output_dicts['mydict' + str(suffix)][k] = v break else: output_dicts['not matched'][k] = v </code></pre> <p>This results in the <code>output_dicts</code> dictionary populated as follows</p> <pre><code>for k, v in output_dicts.iteritems(): print k print v print </code></pre> <p>Which outputs</p> <pre><code>mydict1 {'h18_84pp_3A_MVP2_GoodiesT1-HKJ-DFG-CMVP2_Y1000-FIX.txt': 8, 'h18_84pp_3A_MVP3_GoodiesT1-HKJ-DFG-CMVP3_Y1000-FIX.txt': 9, 'h18_84pp_3A_MVP1_GoodiesT1-HKJ-DFG-CMVP1_Y1000-FIX.txt': 6} mydict0 {'g18_84pp_2A_MVP1_GoodiesT0-HKJ-DFG_MIX-CMVP1_Y1000-MIX.txt': 0, 'g18_84pp_2A_MVP2_GoodiesT0-HKJ-DFG_MIX-CMVP2_Y1000-MIX.txt': 1, 'g18_84pp_2A_MVP4_GoodiesT0-HKJ-DFG_MIX-CMVP4_Y1000-MIX.txt': 3, 'g18_84pp_2A_MVP5_GoodiesT0-HKJ-DFG_MIX-CMVP5_Y1000-MIX.txt': 4, 'g18_84pp_2A_MVP3_GoodiesT0-HKJ-DFG_MIX-CMVP3_Y1000-MIX.txt': 2, 'g18_84pp_2A_MVP6_GoodiesT0-HKJ-DFG_MIX-CMVP6_Y1000-MIX.txt': 5, 'g18_84pp_2A_MVP7_GoodiesT0-HKJ-DFG_MIX-CMVP7_Y1000-MIX.txt': 7} mydict2 {'p18_84pp_2B_MVP1_GoodiesT2-HKJ-DFG-CMVP3_Y1000-FIX.txt': 10} </code></pre>
 

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