Saturday, 15 February 2014

python pattern cutting of strings in a list -



python pattern cutting of strings in a list -

i have dictionary variable "d" key ,an integer, , value list of strings.

368501900 ['gh131.hmm ', 'cbm1.hmm '] 368499531 ['aa8.hmm '] 368500556 ['aa7.hmm '] 368500559 ['gt2.hmm '] 368507728 ['gh16.hmm '] 368496466 ['aa2.hmm '] 368504803 ['gt21.hmm '] 368503093 ['gt1.hmm ', 'gt4.hmm ']

the code this:

d = dict() key in d: dictvalue = d[key] dictmerged = list(sorted(set(dictvalue), key=dictvalue.index)) print (key, dictmerged)

however, want remove string after numbers in lists can have result this:

368501900 ['gh', 'cbm'] 368499531 ['aa'] 368500556 ['aa'] 368500559 ['gt'] 368507728 ['gh'] 368496466 ['aa'] 368504803 ['gt'] 368503093 ['gt']

i think code should inserted between dictvalue , dictmerged, cannot create logic. please, ideas?

import @ beginning

import re

now utilize line between dictvalue , dictmerged

new_dict_value = [re.sub(r'\d.*', '', x) x in dictvalue]

and utilize new_dict_value in next line

python string parsing pattern-matching cut

No comments:

Post a Comment