我读取一个文本文件,从中提取十进制数字作为字符串,并将它们放入一个列表中。
文本文件如下图所示:

我最开始的尝试方法为:

path='./11.Machine-Learning-in-Action-Python3-master/CART_Project1/ex2.txt'
fr=open(path,'r')
all_lines=fr.readlines()
dataset=[]
for line in all_lines:
    line=line.strip().split('\t')
    dataset.append(line)
print(dataset)

输出结果为:

[['0.228628', '-2.266273'], ['0.965969', '112.386764'], ['0.342761', '-31.584855'], ['0.901444', '87.300625'], ['0.585413', '125.295113'], ['0.334900', '18.976650'], ['0.769043', '64.041941'], ['0.297107', '-1.798377'], ['0.901421', '100.133819'], ['0.176523', '0.946348'], ['0.710234', '108.553919'], ['0.981980', '86.399637'], ['0.085873', '-10.137104'], ['0.537834', '90.995536'], ['0.806158', '62.877698'], ['0.708890', '135.416767'], ['0.787755', '118.642009'], ['0.463241', '17.171057'], ['0.300318', '-18.051318'], ['0.815215', '118.319942'], ['0.139880', '7.336784'], ['0.068373', '-15.160836'], ['0.457563', '-34.044555'], ['0.665652', '105.547997'], ['0.084661', '-24.132226'], ['0.954711', '100.935789'], ['0.953902', '130.926480'], ['0.487381', '27.729263'], ['0.759504', '81.106762'], ['0.454312', '-20.360067'], ['0.295993', '-14.988279'], ['0.156067', '7.557349'], ['0.428582', '15.224266'], ['0.847219', '76.240984'], ['0.499171', '11.924204'], ['0.203993', '-22.379119'], ['0.548539', '83.114502'], ['0.790312', '110.159730'], ['0.937766', '119.949824'], ['0.218321', '1.410768'], ['0.223200', '15.501642'], ['0.896683', '107.001620'], ['0.582311', '82.589328'], ['0.698920', '92.470636'], ['0.823848', '59.342323'], ['0.385021', '24.816941'], ['0.061219', '6.695567'], ['0.841547', '115.669032'], ['0.763328', '115.199195'], ['0.934853', '115.753994'], ['0.222271', '-9.255852'], ['0.217214', '-3.958752'], ['0.706961', '106.180427'], ['0.888426', '94.896354'], ['0.549814', '137.267576'], ['0.107960', '-1.293195'], ['0.085111', '37.820659'], ['0.388789', '21.578007'], ['0.467383', '-9.712925'], ['0.623909', '87.181863'], ['0.373501', '-8.228297'], ['0.513332', '101.075609'], ['0.350725', '-40.086564'], ['0.716211', '103.345308'], ['0.731636', '73.912028'], ['0.273863', '-9.457556'], ['0.211633', '-8.332207'], ['0.944221', '100.120253'], ['0.053764', '-13.731698'], ['0.126833', '22.891675'], ['0.952833', '100.649591'], ['0.391609', '3.001104'], ['0.560301', '82.903945'], ['0.124723', '-1.402796'], ['0.465680', '-23.777531'], ['0.699873', '115.586605'], ['0.164134', '-27.405211'], ['0.455761', '9.841938'], ['0.508542', '96.403373'], ['0.138619', '-29.087463'], ['0.335182', '2.768225'], ['0.908629', '118.513475'], ['0.546601', '96.319043'], ['0.378965', '13.583555'], ['0.968621', '98.648346'], ['0.637999', '91.656617'], ['0.350065', '-1.319852'], ['0.632691', '93.645293'], ['0.936524', '65.548418'], ['0.310956', '-49.939516'], ['0.437652', '19.745224'], ['0.166765', '-14.740059'], ['0.571214', '114.872056'], ['0.952377', '73.520802'], ['0.665329', '121.980607'], ['0.258070', '-20.425137'], ['0.912161', '85.005351'], ['0.777582', '100.838446'], ['0.642707', '82.500766'], ['0.885676', '108.045948'], ['0.080061', '2.229873'], ['0.039914', '11.220099'], ['0.958512', '135.837013'], ['0.377383', '5.241196'], ['0.661073', '115.687524'], ['0.454375', '3.043912'], ['0.412516', '-26.419289'], ['0.854970', '89.209930'], ['0.698472', '120.521925'], ['0.465561', '30.051931'], ['0.328890', '39.783113'], ['0.309133', '8.814725'], ['0.418943', '44.161493'], ['0.553797', '120.857321'], ['0.799873', '91.368473'], ['0.811363', '112.981216'], ['0.785574', '107.024467'], ['0.949198', '105.752508'], ['0.666452', '120.014736'], ['0.652462', '112.715799'], ['0.290749', '-14.391613'], ['0.508548', '93.292829'], ['0.680486', '110.367074'], ['0.356790', '-19.526539'], ['0.199903', '-3.372472'], ['0.264926', '5.280579'], ['0.166431', '-6.512506'], ['0.370042', '-32.124495'], ['0.628061', '117.628346'], ['0.228473', '19.425158'], ['0.044737', '3.855393'], ['0.193282', '18.208423'], ['0.519150', '116.176162'], ['0.351478', '-0.461116'], ['0.872199', '111.552716'], ['0.115150', '13.795828'], ['0.324274', '-13.189243'], ['0.446196', '-5.108172'], ['0.613004', '168.180746'], ['0.533511', '129.766743'], ['0.740859', '93.773929'], ['0.667851', '92.449664'], ['0.900699', '109.188248'], ['0.599142', '130.378529'], ['0.232802', '1.222318'], ['0.838587', '134.089674'], ['0.284794', '35.623746'], ['0.130626', '-39.524461'], ['0.642373', '140.613941'], ['0.786865', '100.598825'], ['0.403228', '-1.729244'], ['0.883615', '95.348184'], ['0.910975', '106.814667'], ['0.819722', '70.054508'], ['0.798198', '76.853728'], ['0.606417', '93.521396'], ['0.108801', '-16.106164'], ['0.318309', '-27.605424'], ['0.856421', '107.166848'], ['0.842940', '95.893131'], ['0.618868', '76.917665'], ['0.531944', '124.795495'], ['0.028546', '-8.377094'], ['0.915263', '96.717610'], ['0.925782', '92.074619'], ['0.624827', '105.970743'], ['0.331364', '-1.290825'], ['0.341700', '-23.547711'], ['0.342155', '-16.930416'], ['0.729397', '110.902830'], ['0.640515', '82.713621'], ['0.228751', '-30.812912'], ['0.948822', '69.318649'], ['0.706390', '105.062147'], ['0.079632', '29.420068'], ['0.451087', '-28.724685'], ['0.833026', '76.723835'], ['0.589806', '98.674874'], ['0.426711', '-21.594268'], ['0.872883', '95.887712'], ['0.866451', '94.402102'], ['0.960398', '123.559747'], ['0.483803', '5.224234'], ['0.811602', '99.841379'], ['0.757527', '63.549854'], ['0.569327', '108.435392'], ['0.841625', '60.552308'], ['0.264639', '2.557923'], ['0.202161', '-1.983889'], ['0.055862', '-3.131497'], ['0.543843', '98.362010'], ['0.689099', '112.378209'], ['0.956951', '82.016541'], ['0.382037', '-29.007783'], ['0.131833', '22.478291'], ['0.156273', '0.225886'], ['0.000256', '9.668106'], ['0.892999', '82.436686'], ['0.206207', '-12.619036'], ['0.487537', '5.149336']]

里面的每一项为字符串的格式。

如何将列表中的每个值从字符串转换为浮点?

首先需要明确的是,float的对象不能为列表。

我第一步尝试的方法为:

for i in range(len(dataset)):
    for num in dataset[i]:
       float(num)
print(dataset)

但是问题并没有解决!不起作用。结果与上述相同。

我们尝试第二种方法:

new_dataset=[]
for i in range(len(dataset)):
    for num in dataset[i]:
        new_dataset.append(float(num))
print(new_dataset)

结果如下所示:

[0.228628, -2.266273, 0.965969, 112.386764, 0.342761, -31.584855, 0.901444, 87.300625, 0.585413, 125.295113, 0.3349, 18.97665, 0.769043, 64.041941, 0.297107, -1.798377, 0.901421, 100.133819, 0.176523, 0.946348, 0.710234, 108.553919, 0.98198, 86.399637, 0.085873, -10.137104, 0.537834, 90.995536, 0.806158, 62.877698, 0.70889, 135.416767, 0.787755, 118.642009, 0.463241, 17.171057, 0.300318, -18.051318, 0.815215, 118.319942, 0.13988, 7.336784, 0.068373, -15.160836, 0.457563, -34.044555, 0.665652, 105.547997, 0.084661, -24.132226, 0.954711, 100.935789, 0.953902, 130.92648, 0.487381, 27.729263, 0.759504, 81.106762, 0.454312, -20.360067, 0.295993, -14.988279, 0.156067, 7.557349, 0.428582, 15.224266, 0.847219, 76.240984, 0.499171, 11.924204, 0.203993, -22.379119, 0.548539, 83.114502, 0.790312, 110.15973, 0.937766, 119.949824, 0.218321, 1.410768, 0.2232, 15.501642, 0.896683, 107.00162, 0.582311, 82.589328, 0.69892, 92.470636, 0.823848, 59.342323, 0.385021, 24.816941, 0.061219, 6.695567, 0.841547, 115.669032, 0.763328, 115.199195, 0.934853, 115.753994, 0.222271, -9.255852, 0.217214, -3.958752, 0.706961, 106.180427, 0.888426, 94.896354, 0.549814, 137.267576, 0.10796, -1.293195, 0.085111, 37.820659, 0.388789, 21.578007, 0.467383, -9.712925, 0.623909, 87.181863, 0.373501, -8.228297, 0.513332, 101.075609, 0.350725, -40.086564, 0.716211, 103.345308, 0.731636, 73.912028, 0.273863, -9.457556, 0.211633, -8.332207, 0.944221, 100.120253, 0.053764, -13.731698, 0.126833, 22.891675, 0.952833, 100.649591, 0.391609, 3.001104, 0.560301, 82.903945, 0.124723, -1.402796, 0.46568, -23.777531, 0.699873, 115.586605, 0.164134, -27.405211, 0.455761, 9.841938, 0.508542, 96.403373, 0.138619, -29.087463, 0.335182, 2.768225, 0.908629, 118.513475, 0.546601, 96.319043, 0.378965, 13.583555, 0.968621, 98.648346, 0.637999, 91.656617, 0.350065, -1.319852, 0.632691, 93.645293, 0.936524, 65.548418, 0.310956, -49.939516, 0.437652, 19.745224, 0.166765, -14.740059, 0.571214, 114.872056, 0.952377, 73.520802, 0.665329, 121.980607, 0.25807, -20.425137, 0.912161, 85.005351, 0.777582, 100.838446, 0.642707, 82.500766, 0.885676, 108.045948, 0.080061, 2.229873, 0.039914, 11.220099, 0.958512, 135.837013, 0.377383, 5.241196, 0.661073, 115.687524, 0.454375, 3.043912, 0.412516, -26.419289, 0.85497, 89.20993, 0.698472, 120.521925, 0.465561, 30.051931, 0.32889, 39.783113, 0.309133, 8.814725, 0.418943, 44.161493, 0.553797, 120.857321, 0.799873, 91.368473, 0.811363, 112.981216, 0.785574, 107.024467, 0.949198, 105.752508, 0.666452, 120.014736, 0.652462, 112.715799, 0.290749, -14.391613, 0.508548, 93.292829, 0.680486, 110.367074, 0.35679, -19.526539, 0.199903, -3.372472, 0.264926, 5.280579, 0.166431, -6.512506, 0.370042, -32.124495, 0.628061, 117.628346, 0.228473, 19.425158, 0.044737, 3.855393, 0.193282, 18.208423, 0.51915, 116.176162, 0.351478, -0.461116, 0.872199, 111.552716, 0.11515, 13.795828, 0.324274, -13.189243, 0.446196, -5.108172, 0.613004, 168.180746, 0.533511, 129.766743, 0.740859, 93.773929, 0.667851, 92.449664, 0.900699, 109.188248, 0.599142, 130.378529, 0.232802, 1.222318, 0.838587, 134.089674, 0.284794, 35.623746, 0.130626, -39.524461, 0.642373, 140.613941, 0.786865, 100.598825, 0.403228, -1.729244, 0.883615, 95.348184, 0.910975, 106.814667, 0.819722, 70.054508, 0.798198, 76.853728, 0.606417, 93.521396, 0.108801, -16.106164, 0.318309, -27.605424, 0.856421, 107.166848, 0.84294, 95.893131, 0.618868, 76.917665, 0.531944, 124.795495, 0.028546, -8.377094, 0.915263, 96.71761, 0.925782, 92.074619, 0.624827, 105.970743, 0.331364, -1.290825, 0.3417, -23.547711, 0.342155, -16.930416, 0.729397, 110.90283, 0.640515, 82.713621, 0.228751, -30.812912, 0.948822, 69.318649, 0.70639, 105.062147, 0.079632, 29.420068, 0.451087, -28.724685, 0.833026, 76.723835, 0.589806, 98.674874, 0.426711, -21.594268, 0.872883, 95.887712, 0.866451, 94.402102, 0.960398, 123.559747, 0.483803, 5.224234, 0.811602, 99.841379, 0.757527, 63.549854, 0.569327, 108.435392, 0.841625, 60.552308, 0.264639, 2.557923, 0.202161, -1.983889, 0.055862, -3.131497, 0.543843, 98.36201, 0.689099, 112.378209, 0.956951, 82.016541, 0.382037, -29.007783, 0.131833, 22.478291, 0.156273, 0.225886, 0.000256, 9.668106, 0.892999, 82.436686, 0.206207, -12.619036, 0.487537, 5.149336]

但是这样导致每一行数据区分不明显,很显然也不符合我们的要求。

解决方法

应该这样做:map(float, mylist)

在Python 3中,map不再返回list对象,因此如果我们想要一个新的列表,而不仅仅是要遍历某个列表,那么我们需要:
list(map(float, mylist)

放到上面的问题中,我们的解决方法为:

path='./11.Machine-Learning-in-Action-Python3-master/CART_Project1/ex2.txt'
fr=open(path,'r')
all_lines=fr.readlines()
dataset=[]
for line in all_lines:
    line=line.strip().split('\t')
    dataset.append(list(map(float,line)))
print(dataset)

我们改为:dataset.append(list(map(float,line)))

最为的输出结果为:

[[0.228628, -2.266273], [0.965969, 112.386764], [0.342761, -31.584855], [0.901444, 87.300625], [0.585413, 125.295113], [0.3349, 18.97665], [0.769043, 64.041941], [0.297107, -1.798377], [0.901421, 100.133819], [0.176523, 0.946348], [0.710234, 108.553919], [0.98198, 86.399637], [0.085873, -10.137104], [0.537834, 90.995536], [0.806158, 62.877698], [0.70889, 135.416767], [0.787755, 118.642009], [0.463241, 17.171057], [0.300318, -18.051318], [0.815215, 118.319942], [0.13988, 7.336784], [0.068373, -15.160836], [0.457563, -34.044555], [0.665652, 105.547997], [0.084661, -24.132226], [0.954711, 100.935789], [0.953902, 130.92648], [0.487381, 27.729263], [0.759504, 81.106762], [0.454312, -20.360067], [0.295993, -14.988279], [0.156067, 7.557349], [0.428582, 15.224266], [0.847219, 76.240984], [0.499171, 11.924204], [0.203993, -22.379119], [0.548539, 83.114502], [0.790312, 110.15973], [0.937766, 119.949824], [0.218321, 1.410768], [0.2232, 15.501642], [0.896683, 107.00162], [0.582311, 82.589328], [0.69892, 92.470636], [0.823848, 59.342323], [0.385021, 24.816941], [0.061219, 6.695567], [0.841547, 115.669032], [0.763328, 115.199195], [0.934853, 115.753994], [0.222271, -9.255852], [0.217214, -3.958752], [0.706961, 106.180427], [0.888426, 94.896354], [0.549814, 137.267576], [0.10796, -1.293195], [0.085111, 37.820659], [0.388789, 21.578007], [0.467383, -9.712925], [0.623909, 87.181863], [0.373501, -8.228297], [0.513332, 101.075609], [0.350725, -40.086564], [0.716211, 103.345308], [0.731636, 73.912028], [0.273863, -9.457556], [0.211633, -8.332207], [0.944221, 100.120253], [0.053764, -13.731698], [0.126833, 22.891675], [0.952833, 100.649591], [0.391609, 3.001104], [0.560301, 82.903945], [0.124723, -1.402796], [0.46568, -23.777531], [0.699873, 115.586605], [0.164134, -27.405211], [0.455761, 9.841938], [0.508542, 96.403373], [0.138619, -29.087463], [0.335182, 2.768225], [0.908629, 118.513475], [0.546601, 96.319043], [0.378965, 13.583555], [0.968621, 98.648346], [0.637999, 91.656617], [0.350065, -1.319852], [0.632691, 93.645293], [0.936524, 65.548418], [0.310956, -49.939516], [0.437652, 19.745224], [0.166765, -14.740059], [0.571214, 114.872056], [0.952377, 73.520802], [0.665329, 121.980607], [0.25807, -20.425137], [0.912161, 85.005351], [0.777582, 100.838446], [0.642707, 82.500766], [0.885676, 108.045948], [0.080061, 2.229873], [0.039914, 11.220099], [0.958512, 135.837013], [0.377383, 5.241196], [0.661073, 115.687524], [0.454375, 3.043912], [0.412516, -26.419289], [0.85497, 89.20993], [0.698472, 120.521925], [0.465561, 30.051931], [0.32889, 39.783113], [0.309133, 8.814725], [0.418943, 44.161493], [0.553797, 120.857321], [0.799873, 91.368473], [0.811363, 112.981216], [0.785574, 107.024467], [0.949198, 105.752508], [0.666452, 120.014736], [0.652462, 112.715799], [0.290749, -14.391613], [0.508548, 93.292829], [0.680486, 110.367074], [0.35679, -19.526539], [0.199903, -3.372472], [0.264926, 5.280579], [0.166431, -6.512506], [0.370042, -32.124495], [0.628061, 117.628346], [0.228473, 19.425158], [0.044737, 3.855393], [0.193282, 18.208423], [0.51915, 116.176162], [0.351478, -0.461116], [0.872199, 111.552716], [0.11515, 13.795828], [0.324274, -13.189243], [0.446196, -5.108172], [0.613004, 168.180746], [0.533511, 129.766743], [0.740859, 93.773929], [0.667851, 92.449664], [0.900699, 109.188248], [0.599142, 130.378529], [0.232802, 1.222318], [0.838587, 134.089674], [0.284794, 35.623746], [0.130626, -39.524461], [0.642373, 140.613941], [0.786865, 100.598825], [0.403228, -1.729244], [0.883615, 95.348184], [0.910975, 106.814667], [0.819722, 70.054508], [0.798198, 76.853728], [0.606417, 93.521396], [0.108801, -16.106164], [0.318309, -27.605424], [0.856421, 107.166848], [0.84294, 95.893131], [0.618868, 76.917665], [0.531944, 124.795495], [0.028546, -8.377094], [0.915263, 96.71761], [0.925782, 92.074619], [0.624827, 105.970743], [0.331364, -1.290825], [0.3417, -23.547711], [0.342155, -16.930416], [0.729397, 110.90283], [0.640515, 82.713621], [0.228751, -30.812912], [0.948822, 69.318649], [0.70639, 105.062147], [0.079632, 29.420068], [0.451087, -28.724685], [0.833026, 76.723835], [0.589806, 98.674874], [0.426711, -21.594268], [0.872883, 95.887712], [0.866451, 94.402102], [0.960398, 123.559747], [0.483803, 5.224234], [0.811602, 99.841379], [0.757527, 63.549854], [0.569327, 108.435392], [0.841625, 60.552308], [0.264639, 2.557923], [0.202161, -1.983889], [0.055862, -3.131497], [0.543843, 98.36201], [0.689099, 112.378209], [0.956951, 82.016541], [0.382037, -29.007783], [0.131833, 22.478291], [0.156273, 0.225886], [0.000256, 9.668106], [0.892999, 82.436686], [0.206207, -12.619036], [0.487537, 5.149336]]

我们的问题得到完美解决!

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐