user15649753
user15649753

Reputation:

how I get a part of elements of a list?

I have a list. the first 4 elements of the list are:

H=['     2 t[0,1]       B          1e-08                             \n',
 '     3 t[0,2]       B          1e-09                             \n',
 '     4 t[0,3]       B          1e-2                             \n',
 '     5 t[0,4]       B          1e-08                             \n']

how I can get just the following:

output:

H=[1e-08,1e-09,1e-2,1e-08]

Upvotes: 0

Views: 67

Answers (1)

Mark
Mark

Reputation: 92461

strip() the strings of ending white space, then split and take the column you need:

H = ['     2 t[0,1]       B          1e-08                             \n',
     '     3 t[0,2]       B          1e-09                             \n',
     '     4 t[0,3]       B          1e-2                             \n',
     '     5 t[0,4]       B          1e-08                             \n'
    ]

[s.split()[3] for s in map(str.strip, H)]
# ['1e-08', '1e-09', '1e-2', '1e-08']

If you want actual numbers instead of strings, you can pass the values to float()

[float(s.split()[3]) for s in map(str.strip, H)]
# [1e-08, 1e-09, 0.01, 1e-08]

Upvotes: 4

Related Questions