python - How to extract the common words before particular symbol and find particular word -


if have dictionary:

mydict = {"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_mvp3_goodiest0-hkj-dfg_mix-cmvp3_y1000-mix.txt" : 2,           "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_mvp6_goodiest0-hkj-dfg_mix-cmvp6_y1000-mix.txt" : 5,           "h18_84pp_3a_mvp1_goodiest1-hkj-dfg-cmvp1_y1000-fix.txt" : 6,           "g18_84pp_2a_mvp7_goodiest0-hkj-dfg_mix-cmvp7_y1000-mix.txt" : 7,           "h18_84pp_3a_mvp2_goodiest1-hkj-dfg-cmvp2_y1000-fix.txt" : 8,           "h18_84pp_3a_mvp3_goodiest1-hkj-dfg-cmvp3_y1000-fix.txt" : 9,           "p18_84pp_2b_mvp1_goodiest2-hkj-dfg-cmvp3_y1000-fix.txt" : 10} 
  1. i want extract common part g18_84pp_2a_mvp_goodiest0 before first -.

  2. also want add _mix follow g18_84pp_2a_mvp_goodiest0 when finding particular word mix in first group . assume able classify 2 groups depending on whether mix or fix in mydict, final output dictionary:

outputnamedict= {"g18_84pp_2a_mvp_goodiest0_mix" : 0,                   "h18_84pp_3a_mvp_goodiest1_fix" : 1,                   "p18_84pp_2b_mvp_fix": 2} 

is there function use find common part? how pick word before or after particular symbol - , find particular words mix or fix?

you can use split common part:

s = "g18_84pp_2a_mvp1_goodiest0-hkj-dfg_mix-cmvp1_y1000-mix.txt" n = s.split('-')[0] 

in fact, split give list of each token delimited '-', s.split('-') yields:

['g18_84pp_2a_mvp1_goodiest0', 'hkj', 'dfg_mix', 'cmvp1_y1000', 'mix.txt'] 

to see if mix or fix in string, can use in:

if 'mix' in s:     print "then mix in string s" 

if want rid if numbers after 'mvp', can use re module:

import re s = 'g18_84pp_2a_mvp1_goodiest0' s = re.sub('mvp[0-9]*','mvp',s) 

here sample function list of common parts:

def foo(mydict):     return [re.sub('mvp[0-9]*', 'mvp', k.split('-')[0]) k in mydict] 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -