POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500000.00000\n', 'TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500020.00..." />

      asked    Chad     2018-10-22       python       92 view        2 Answers

[SOLVED] How to retrieve specific information from a list in python?

I have a file from which I would like to retrieve specific information.

First, I extracted the lines of interest and I put them in a list:

array = []
file_in = open("Traj.pdb", "r")
  for line in file_in:
      if line.startswith('TITLE'):
          array.append(line)

I end up with a list like this:

['TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500000.00000\n', 'TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500020.00000\n', 'TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500040.00000\n']

I would like to extract the "t=" information (t= 1500000.00000, t= 1500020.00000, t= 1500040.00000 ...etc) but I don't know how to do it. As you can see, the elements of my list are sentences and I'm a little bit confused on how I can retrieve the specific information in this case. I would really appreciate your help or advice.

  2 Answers  

        answered    Hannah     2018-10-22      

split your list on t=, it gives you left and right halves. You only need the right half i.e content after t=. Also, make sure that you remove the new line character \n at the end of the right half.

l = ['TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500000.00000\n', 'TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500020.00000\n', 'TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500040.00000\n']
result = []
for line in l:
    splitted_arr = line.split("t= ")
    # Consider only the right half
    splitted_arr = splitted_arr[1]
    # Remove the last character i.e \n
    splitted_arr = splitted_arr[:len(splitted_arr)-1]
    result.append(splitted_arr)
print(result)


        answered    Parker     2018-10-22      

Try this:

import re

array_with_elements_i_want = []

for elem in array:
    array_with_elements_i_want.append(re.search(r"t= (.)*", elem).group(0))

Or even better:

import re

array_with_elements_i_want = []

for elem in array:
    v = re.search(r"t= (.)*", elem)
    if v is not None:
        array_with_elements_i_want.append(v.group(0))




Your Answer





 2018-10-22         Jean

How to inject the username (not the Authentication)?

In my spring boot application, in my rest controllers, I successfully inject an instance of Authentication to get the session's user information.However, in all of those controllers, I currently call a helper method like this:@GetMappingpublic List<String> getData(Authentication auth) { String username = auth.getName().replaceFirst(".*?\\\\", ""); // to remove windows domain name // the rest of these methods only use variable `username`, never `auth`}How can I reduce this code duplication? You can create a utility class as follows and then you can use it like...
 java                     1 answers                     71 view
 2018-10-22         Simon

Negative Binomial Loss in Neural Network using Tensorflow / Keras

I'm working with a highly skewed count dataset that's typically fit with a negative binomial. I want to use the negative binomial as a loss functions in Keras or Tensorflow on a feed forward neural network. To my knowledge, after looking through available loss functions, such a function doesn't exist for keras or tensorflow (although I'm hoping I'm wrong and I just missed something). I've looked around and I've seen posts suggest just swapping out the loss to include the negative binomial, but it appears there's more to creating a custom loss in tensorflow than just swappin...
 python                     1 answers                     71 view
 2018-10-22         Rose

T-SQL Combine rows in continuation

I have a table that looks like the following.What I want is the the rows in continuation of each other to be grouped together - for each "ID".The column IsContinued marks if the next row should be combined with the current rowMy data looks like this:+-----+--------+-------------+-----------+----------+| ID | Period | IsContinued | StartDate | EndDate |+-----+--------+-------------+-----------+----------+| 123 | 1 | 1 | 20180101 | 20180404 |+-----+--------+-------------+-----------+----------+| 123 | 2 | 1 | 20180501 | 20180910 |+-----+-----...
 sql                     1 answers                     73 view