将列表中的字符串转换为浮点数 [英] convert strings in list to float

查看:303
本文介绍了将列表中的字符串转换为浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题让我抓狂.我有一个从网站上报废的数据并将这些数据放入字典中.因此,我有几个列表,其中一个列表如下所示:

<预> <代码> '在':[ '7,051,156,075,145', '878009569197', '427386441994', '278189230134', '230599954634', '197088252840', '101610549933','78,426830219' ,'80,925933532' ,'58,451193176','55,701282247' ,'49,748756546' ,'48,642591960' ,'45,686162172' ,'44,227235911' ,'40,467951256' ,'16,392881465' , '5988546624','41,810356569' ,'23,515110330' ,'35,815116718' ,'10,968016226' ,'518858345' ,'29,947210177' ,'29,030975280' ,'28,803225552' , '373570428','27,527784709' , '373964822', '514671410','25,875702735' , '416462736','24,423209779' ,'24,332893924' ,'22,491450198','22,894037015' ,'23,026866310' , '6148324700','22,226875309' ,'21,127010221' , '375662568','18,8453​​30059' , '238084409','18,338638037' , '6469472952','16,988637757' , '234705103','16164528769' , '236542082','15,878894181' ,'15,892415892' , '384601333', '173719914','14,374301195' ,'13,789745661' ,'13,333600469",'12,935822692' , '1414494923','13,000908688' , '2875324761', '280912611','12,443874812' ,'12,470333848' , '188668181','12,092658438' , '676583644','11,997025285' ,'11,677854811' ,'220087430','11,251777539' ,'11,442705899' , '8628429553', '190648851','11,187421523' , '3684540569','10,670576444' ,'10,740578885' ,'10,582331778' ,'10,557152315' , '9804556177','10,325762681",'10,193777314' ,'10,241020644' ,'10,218671348' , '5565872689', '6066496977', '128971640', '160853134', '3061365095', '8849393167', '182484904', '161406328', '9335264956',158941175",8893005099",132642660",147492645",133898533",8565414335",8543285361",1081514186",8010900010",344032888",7851320645",119252217",7708770926' '3831828937', '266060360', '7469255927', '2553584433', '7404456294', '1775993183', '7338693939', '7337702662', '7246023792',' 3,147,875441' ,‘142555296’,‘1953694528’,‘3918267288’,‘1324557844’,‘5683622890’,‘6927422982’,‘106687337’,‘6912850849’,‘2845801508’,‘6818774192’,‘6853915064’,‘147347763’, '344146667', '6711901497', '6570349311', '6519300790', '135371330', '6472184188','84,726075' , '6224918718', '5795088428', '5348330674','76,438957' , '6156100475','6046328039' , '1572859369', '5966535367', '5960854825', '5844987758','99,526367' , '3320692742', '5763785447', '332891989', '5673010795', '2120698374', '5600425762', '3406789774']

值已存储为字符串,我认为可以直接将其转换为浮点数,但我不知道如何转换.没关系,我以为我可以将这些值转换为浮点数并进一步处理它们.但是我无法让它工作.我将列表分配给变量bob"并尝试使用以下代码转换为浮点数:

empty = []der = np.array(空,dtype = np.float32)der = np.append(der, bob)

当我尝试

print(der/2)

它给了我这个:类型错误:输入类型不支持 ufunc 'true_divide',并且无法根据转换规则 ''safe'' 将输入安全地强制转换为任何支持的类型"

这是怎么回事?我的错误在哪里?

感谢您的帮助!谢谢

解决方案

你可以使用 map 和 lambda 来去除逗号并将字符串浮点数列表转换为浮点数列表

result = list(map(lambda x: float(x.replace(",", "")), list_of_string_floats))

对于整数来说是

result = list(map(lambda x: int(x.replace(",", "")), list_of_string_ints))

没必要用numpy

this issue is driving me nuts. I have a scrapped data from a website and put those data into a dictionary. As a result I have a couple of lists and one of those lists looks like this:

    'In': ['7,051,156,075,145', '878,009,569,197', '427,386,441,994', '278,189,230,134', '230,599,954,634', '197,088,252,840', '101,610,549,933', '78,426,830,219', '80,925,933,532', '58,451,193,176', '55,701,282,247', '49,748,756,546', '48,642,591,960', '45,686,162,172', '44,227,235,911', '40,467,951,256', '16,392,881,465', '5,988,546,624', '41,810,356,569', '23,515,110,330', '35,815,116,718', '10,968,016,226', '518,858,345', '29,947,210,177', '29,030,975,280', '28,803,225,552', '373,570,428', '27,527,784,709', '373,964,822', '514,671,410', '25,875,702,735', '416,462,736', '24,423,209,779', '24,332,893,924', '22,491,450,198', '22,894,037,015', '23,026,866,310', '6,148,324,700', '22,226,875,309', '21,127,010,221', '375,662,568', '18,845,330,059', '238,084,409', '18,338,638,037', '6,469,472,952', '16,988,637,757', '234,705,103', '16,164,528,769', '236,542,082', '15,878,894,181', '15,892,415,892', '384,601,333', '173,719,914', '14,374,301,195', '13,789,745,661', '13,333,600,469', '12,935,822,692', '1,414,494,923', '13,000,908,688', '2,875,324,761', '280,912,611', '12,443,874,812', '12,470,333,848', '188,668,181', '12,092,658,438', '676,583,644', '11,997,025,285', '11,677,854,811', '220,087,430', '11,251,777,539', '11,442,705,899', '8,628,429,553', '190,648,851', '11,187,421,523', '3,684,540,569', '10,670,576,444', '10,740,578,885', '10,582,331,778', '10,557,152,315', '9,804,556,177', '10,325,762,681', '10,193,777,314', '10,241,020,644', '10,218,671,348', '5,565,872,689', '6,066,496,977', '128,971,640', '160,853,134', '3,061,365,095', '8,849,393,167', '182,484,904', '161,406,328', '9,335,264,956', '158,941,175', '8,893,005,099', '132,642,660', '147,492,645', '133,898,533', '8,565,414,335', '8,543,285,361', '1,081,514,186', '8,010,900,010', '344,032,888', '7,851,320,645', '119,252,217', '7,708,770,926', '3,831,828,937', '266,060,360', '7,469,255,927', '2,553,584,433', '7,404,456,294', '1,775,993,183', '7,338,693,939', '7,337,702,662', '7,246,023,792', '3,147,875,441', '142,555,296', '1,953,694,528', '3,918,267,288', '1,324,557,844', '5,683,622,890', '6,927,422,982', '106,687,337', '6,912,850,849', '2,845,801,508', '6,818,774,192', '6,853,915,064', '147,347,763', '344,146,667', '6,711,901,497', '6,570,349,311', '6,519,300,790', '135,371,330', '6,472,184,188', '84,726,075', '6,224,918,718', '5,795,088,428', '5,348,330,674', '76,438,957', '6,156,100,475', '6,046,328,039', '1,572,859,369', '5,966,535,367', '5,960,854,825', '5,844,987,758', '99,526,367', '3,320,692,742', '5,763,785,447', '332,891,989', '5,673,010,795', '2,120,698,374', '5,600,425,762', '3,406,789,774']

The values have been stored as strings which I think could be directly converted to floats but I dont know how. Nevermind, I thought I could just convert those values into floats and work with them further. However I cant get it to work. I assigned the list to the variable "bob" and tried the following code to convert to float:

empty = []
der = np.array(empty, dtype = np.float32)
der = np.append(der, bob)

When i try

print(der/2)

it gives me this: "TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''"

whats going on here? Where is my mistake?

I appreciate any help! Thanks

解决方案

you can use map and lambda to remove comma and convert the list of string floats to list of floats

result = list(map(lambda x: float(x.replace(",", "")), list_of_string_floats))

for integers it would be

result = list(map(lambda x: int(x.replace(",", "")), list_of_string_ints))

There is no need to use numpy

这篇关于将列表中的字符串转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆