>>> out = []
>>> for s in l:
out.append( float(re.findall('\d+(?:\.\d+)?', s)[0]) )
>>> out
=> [7.2, 8.0]
或者,不使用regex,使用split
>>> out = []
>>> for s in l:
num = s.replace(' ','').split('=')[1]
#note : removed whitespace so don't have to deal with cases like
# `n = 2` or `n=2`
out.append(float(num))
>>> out
=> [7.2, 8.0]
最好使用字典来管理与值关联的标签,但是,如果您必须将每个label=value对作为列表中的条目,因为您可能正在从其他地方将其读入Python,则可以使用
re
模块从列表中的每个字符串中提取数值:使用^{} :
或者,不使用
regex
,使用split
#驱动程序值:
这个怎么样
输出:
相关问题 更多 >
编程相关推荐