Python将带有字符串的列表的格式更改为浮于列中

时间:2019-04-11 10:34:54

标签: python string list format

使用字符串更改列表

尝试过np.array

with open(file_to_open) as f:
    for line in f:
        # split the line
        line = line.strip()
        columns = line.split(",")
        if columns[0] == "1":
               x_train.append(line)
        if columns[0] == "2":
             y_train.append(line)
                #print(line, end='')
        print( x_train)

我得到这个结果:

['1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065', '1,13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050', '1,13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185', '1,14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480', '1,13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735', '1,14.2,1.76,2.4 ....]

但是我希望这样:

1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065
1,13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050
1,13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185
1,14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480
1,13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735
1,14.2,1.76,2.45,15.2,112,3.27,3.39,.34,1.97,6.75,1.05,2.85,1450
1,14.39,1.87,2.45,14.6,96,2.5,2.52,.3,1.98,5.25,1.02,3.58,1290
1,14.06,2.15,2.61,17.6,121,2.6,2.51,.31,1.25,5.05,1.06,3.58,1295
1,14.83,1.64,2.17,14,97,2.8,2.98,.29,1.98,5.2,1.08,2.85,1045
1,13.86,1.35,2.27,16,98,2.98,3.15,.22,1.85,7.22,1.01,3.55,1045
1,14.1,2.16,2.3,18,105,2.95,3.32,.22,2.38,5.75,1.25,3.17,1510
1,14.12,1.48,2.32,16.8,95,2.2,2.43,.26,1.57,5,1.17,2.82,1280
1,13.75,1.73,2.41,16,89,2.6,2.76,.29,1.81,5.6,1.15,2.9,1320

那是txt文件负载的一部分:

1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065 1,13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050 1,13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185 1,14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480 1,13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735 1,14.2,1.76,2.45,15.2,112,3.27,3.39,.34,1.97,6.75,1.05,2.85,1450 1,14.39,1.87,2.45,14.6,96,2.5,2.52,.3,1.98,5.25,1.02,3.58,1290 1,14.06,2.15,2.61,17.6,121,2.6,2.51,.31,1.25,5.05,1.06,3.58,1295

3 个答案:

答案 0 :(得分:1)

您正在将字符串追加到列表中,所以毫不奇怪,您得到的是字符串列表。您不清楚如何使用np.array -在您的代码中看不到任何迹象-您也没有清楚地知道要使用哪种实际数据格式。

因此,我假设您想要x_trainy_train变量中的列表列表。而不是添加一行,而是添加一个转换为浮点数的列列表:

    ...
    x_train.append([float(x) for x in columns])
    ...
    y_train.append([float(x) for x in columns])
    ...

然后将x_trainy_train转换为numpy nd_arrays或以所需的格式打印每一行应该很容易...

答案 1 :(得分:0)

如果您确定列表中的所有元素都是数字,并且没有其他字母值,则可以对代码进行调整,以从字符串类型转换为浮点数,并包含以下行:

[float(element) for element in columns]

在您的代码中,您可以像这样使用它:

if columns[0] == "1":
    x_train.append([float(element) for element in columns])

if columns[0] == "2":
    y_train.append([float(element) for element in columns])

答案 2 :(得分:0)

既然看起来像是逗号分隔的文件,则可以尝试:

data = ps.read_csv('train.txt', header=None)
x_train = data[data.iloc[:,0]==1]
y_train = data[data.iloc[:,0]==2]

如果您希望从代码中进行尝试,请尝试以下操作:

np.array(list(map(float, ','.join(s).split(',')))).reshape(len(x_train),14)