在Python中将Fractions列表转换为浮点数

use*_*392 8 python floating-point fractions

我有一个分数列表,例如:

data = ['24/221 ', '25/221 ', '24/221 ', '25/221 ', '25/221 ', '30/221 ', '31/221 ', '31/221 ', '31/221 ', '31/221 ', '30/221 ', '30/221 ', '33/221 ']
Run Code Online (Sandbox Code Playgroud)

我将如何将这些转换为浮点数,例如

data = ['0.10 ', '0.11 ', '0.10 ', '0.11 ', '0.13 ', '0.14 ', '0.14 ', '0.14 ', '0.14 ', '0.14 ', '0.13 ', '0.13 ', '0.15 ']
Run Code Online (Sandbox Code Playgroud)

Fraction模块似乎只转换为Fractions(而不是)并float([x])需要一个字符串或整数.

car*_*arl 13

import fractions
data = [float(fractions.Fraction(x)) for x in data]
Run Code Online (Sandbox Code Playgroud)

或者完全匹配你的例子(数据以字符串结尾):

import fractions
data = [str(float(fractions.Fraction(x))) for x in data]
Run Code Online (Sandbox Code Playgroud)