[英]Take a specific sub-string from a string in python
How to store lst1 = [26.7,8.2,13.7,8.6,16]
from Train_level1 in python
? 如何在
python
从Train_level1存储lst1 = [26.7,8.2,13.7,8.6,16]
?
Train_level1 = ['1_Rehana_Karim_F49_850_GL=26.7','43_G.M.Alauddin Azad_M42_940_GL=8.2','110_Ronojeet_Bishwash_M47_940_GL=13.7','112_Mustafizur_Rahman_M60_850_GL=8.6','123_Farida_Yeasmin_F55_940_GL=16']
Similarly, How to store lst2 = [11.5,12.9,9.2]
from Train_level2 in python
? 同样,如何在
python
从Train_level2存储lst2 = [11.5,12.9,9.2]
?
Train_level2 = ['S140_M75_GL=11.5-850LEDFon_F=110.jpg', 'S85_F56_GL=12.9-940LEDFon_F=105.jpg', 'S108_M71_GL=9.2-850LEDFon_F=100.jpg']
For Train_level
, you need to get the number after the =
, so we use split()
on the list of strings to split it by the =
character and get the second string that is in index 0: 对于
Train_level
,您需要获取=
后面的数字,因此我们在字符串列表上使用split()
将其按=
字符进行拆分,并获取索引为0的第二个字符串:
lst1 = [float(train.split('=')[1]) for train in Train_level1]
For Train_level2
, it's similar, except we need to do two splits - first by =
and get the second string (index 1), and then by -
and get the first string (index 0): 对于
Train_level2
,它是相似的,除了我们需要进行两次拆分-首先通过=
并获取第二个字符串(索引1),然后通过-
并获取第一个字符串(索引0):
lst2 = [float(train.split('=')[1].split('-')[0]) for train in Train_level2]
We use float()
on the result since split
returns a string but the output is a list of numbers, not strings. 我们在结果上使用
float()
,因为split
返回一个字符串,但输出是数字列表,而不是字符串。 float
will convert a decimal string that contains a number, to a floating point number. float
会将包含数字的十进制字符串转换为浮点数。
You can use regex to parse your numbers: 您可以使用正则表达式解析数字:
import re
Train_level1 = ['1_Rehana_Karim_F49_850_GL=26.7',
'43_G.M.Alauddin Azad_M42_940_GL=8.2',
'110_Ronojeet_Bishwash_M47_940_GL=13.7',
'112_Mustafizur_Rahman_M60_850_GL=8.6',
'123_Farida_Yeasmin_F55_940_GL=16']
Train_level2 = ['S140_M75_GL=11.5-850LEDFon_F=110.jpg',
'S85_F56_GL=12.9-940LEDFon_F=105.jpg',
'S108_M71_GL=9.2-850LEDFon_F=100.jpg']
def parseIt(data):
p1 = r"=(\d+\.?\d*)" # find '=' followed numbers followed by optional '.' + more numbers
return [float(x[0]) for x in (re.findall(p1,y) for y in data) if x]
print(parseIt(Train_level1))
print(parseIt(Train_level2))
Output: 输出:
[26.7, 8.2, 13.7, 8.6, 16.0]
[11.5, 12.9, 9.2]
The list-comp and regex are identical, hence I created a function for it. list-comp和regex是相同的,因此我为此创建了一个函数。 They apply the regex to each list-element.
他们将正则表达式应用于每个列表元素。 You only have one
=99.99
element per list so that one we take and change into a float. 每个列表只有一个
=99.99
元素,因此我们将其中一个元素更改为浮动元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.