4

How to get all the values in between 'uniprotkb:' and '(gene name)' in the 'str' below:

str = 'uniprotkb:HIST1H3D(gene name)|uniprotkb:HIST1H3A(gene name)|uniprotkb:HIST1H3B(gene name)|uniprotkb:HIST1H3C(gene name)|uniprotkb:HIST1H3E(gene name)|uniprotkb:HIST1H3F(gene name)|uniprotkb:HIST1H3G(gene name)|uniprotkb:HIST1H3H(gene name)|uniprotkb:HIST1H3I(gene name)|uniprotkb:HIST1H3J(gene name)' 

The result is:

HIST1H3D
HIST1H3A
HIST1H3B
HIST1H3C
HIST1H3E
HIST1H3F
HIST1H3G
HIST1H3H
HIST1H3I
HIST1H3J 
1
  • 11
    Please don't name a variable 'str' -- you'll hide the built-in string class Commented Oct 2, 2012 at 14:51

3 Answers 3

8

Using re.findall(), you can get all parts of a string that match a regular expression:

>>> import re
>>> sstr = 'uniprotkb:HIST1H3D(gene name)|uniprotkb:HIST1H3A(gene name)|uniprotkb:HIST1H3B(gene name)|uniprotkb:HIST1H3C(gene name)|uniprotkb:HIST1H3E(gene name)|uniprotkb:HIST1H3F(gene name)|uniprotkb:HIST1H3G(gene name)|uniprotkb:HIST1H3H(gene name)|uniprotkb:HIST1H3I(gene name)|uniprotkb:HIST1H3J(gene name)' 
>>> re.findall(r'uniprotkb:([^(]*)\(gene name\)', sstr)

['HIST1H3D', 'HIST1H3A', 'HIST1H3B', 'HIST1H3C', 'HIST1H3E', 'HIST1H3F', 'HIST1H3G', 'HIST1H3H', 'HIST1H3I', 'HIST1H3J']
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a oneliner:

astr = 'uniprotkb:HIST1H3D(gene name)|uniprotkb:HIST1H3A(gene name)|uniprotkb:HIST1H3B(gene name)|uniprotkb:HIST1H3C(gene name)|uniprotkb:HIST1H3E(gene name)|uniprotkb:HIST1H3F(gene name)|uniprotkb:HIST1H3G(gene name)|uniprotkb:HIST1H3H(gene name)|uniprotkb:HIST1H3I(gene name)|uniprotkb:HIST1H3J(gene name)'
[pt.split('(')[0] for pt in astr.strip().split('uniprotkb:')][1:]

Gives:

['HIST1H3D',
 'HIST1H3A',
 'HIST1H3B',
 'HIST1H3C',
 'HIST1H3E',
 'HIST1H3F',
 'HIST1H3G',
 'HIST1H3H',
 'HIST1H3I',
 'HIST1H3J']

I don't recommend regexp solutions, if runtime matters.

Comments

-1

I wouldn't bother with a regular expression:

s = 'uniprotkb:HIST1H3D(gene name)|uniprotkb:HIST1H3A(gene name)'  # etc

gene_names = []
for substring in s.split('|'):
    removed_first = substring.partition('uniprotkb:')[2]  # remove the first part of the substring
    removed_second = removed_first.partition('(gene name)')[0]  # remove the second part
    gene_names.append(removed_second)  # put it on the list

should do the trick. You could even one-liner it - the above is equivalent to:

gene_names = [substring.partition('uniprotkb:')[2].partition('(gene name)')[0] for substring in s.split('|')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.