简体   繁体   English

将分数列表转换为数值数组

[英]Convert list of fractions to numerical array

I have data in the form of a list that looks like this:我有一个列表形式的数据,如下所示:

'2/7,5/7,0,0,0,0,0,0,1,8131435000/20657804439,12526369439/20657804439,46/51,5/51,47240285000/61973413317,14733128317/61973413317\n',
'2/7,5/7,0,0,0,0,0,0,1,1304992/3861431,2556439/3861431,46/51,5/51,8805032/11584293,2779261/11584293\n',
'2/7,5/7,0,0,0,0,0,0,1,251888/1212389,960501/1212389,46/51,5/51,3238088/3637167,399079/3637167\n'

How do I convert this to a matrix of numbers?如何将其转换为数字矩阵? Preferably using decimals instead of fractions.最好使用小数而不是分数。

Here is the code:这是代码:

lines = [
    '2/7,5/7,0,0,0,0,0,0,1,8131435000/20657804439,12526369439/20657804439,46/51,5/51,47240285000/61973413317,14733128317/61973413317\n',
    '2/7,5/7,0,0,0,0,0,0,1,1304992/3861431,2556439/3861431,46/51,5/51,8805032/11584293,2779261/11584293\n',
    '2/7,5/7,0,0,0,0,0,0,1,251888/1212389,960501/1212389,46/51,5/51,3238088/3637167,399079/3637167\n'
]

matrix = [[eval(x) for x in line.strip().split(',')] 
                   for line in lines]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM