I want to extract lines in a list that contain carbons ('C').
The actual lines are:
propene_data = ['H -0.08677109049370 0.00000005322169 0.02324774260533\n', 'C -0.02236345244409 -0.00000001742911 1.09944502076327\n', 'C 1.14150994274008 0.00000000299501 1.72300489107368\n', 'H -0.95761218150040 -0.00000002374717 1.63257861279343\n', 'H 1.17043966864771 0.00000000845005 2.80466760537188\n', 'C 2.46626448549704 -0.00000000616665 1.02315746104893\n', 'H 3.28540550052797 0.00000001315434 1.73628424885091\n', 'H 2.55984407099540 -0.87855375749407 0.38655722260408\n', 'H 2.55984405602998 0.87855372701591 0.38655719488850\n']
I've tried to extract the carbons line using the following solution;
car1 = propene_data[1].split()
car2 = propene_data[2].split()
car3 = propene_data[5].split()
propene_carbons = car1 + car2 + car3
This solution gives;
propene_carbons = ['C', '-0.02236345244409', '-0.00000001742911', '1.09944502076327', 'C', '1.14150994274008', '0.00000000299501', '1.72300489107368', 'C', '2.46626448549704', '-0.00000000616665', '1.02315746104893']
It gives what I want, but I would like to know if I could indexing instead (in case the list is much longer). How do I use indexing in this case?
What you need here is startswith
:
result = text.startswith('C')
in loop:
result = [i for i in propene_data if i.startswith('C')]
Output:
['C -0.02236345244409 -0.00000001742911 1.09944502076327\n', 'C 1.14150994274008 0.00000000299501 1.72300489107368\n', 'C 2.46626448549704 -0.00000000616665 1.02315746104893\n']