Python教程

Python删除字符串中指定字符,将字符串按空格切割并存储在数组中,将字符串数组转化为float型数组

本文主要是介绍Python删除字符串中指定字符,将字符串按空格切割并存储在数组中,将字符串数组转化为float型数组,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
originalArr = "[-0.34985686  0.29479297 -0.27478726  0.07474025  0.01653126  0.59077567 0.82123364 -0.34997168 -0.00697975  0.20301832  0.73513871  0.05697672 0.50133399  0.08622986  0.09953928  0.46035864  0.82622047  0.79236972 0.63738386  0.8267959   0.22646075 -0.0599288  -0.06546499 -0.61105956 0.61913797  0.43338787  0.84928721 -0.62307324  0.93602742  0.27061763 -0.01883277  0.66671322  0.4175206  -0.41437707 -0.19481738 -0.5664881 -0.94443227  0.66710114 -0.02104479 -0.62232492  0.1199961   0.20708077  0.70564915 -0.35685799 -0.19080333]"
originalArr = originalArr.strip('[]')  # 删除字符串中的 "["  或  "]" 字符
num = [str(n) for n in originalArr.split()]  # 将字符串按照空格切割并存储在数组中
arr = [ float(x) for x in num ]  # 将字符串数组转化维float型数组
print(num)
print(arr)

输出结果:

['-0.34985686', '0.29479297', '-0.27478726', '0.07474025', '0.01653126', '0.59077567', '0.82123364', '-0.34997168', '-0.00697975', '0.20301832', '0.73513871', '0.05697672', '0.50133399', '0.08622986', '0.09953928', '0.46035864', '0.82622047', '0.79236972', '0.63738386', '0.8267959', '0.22646075', '-0.0599288', '-0.06546499', '-0.61105956', '0.61913797', '0.43338787', '0.84928721', '-0.62307324', '0.93602742', '0.27061763', '-0.01883277', '0.66671322', '0.4175206', '-0.41437707', '-0.19481738', '-0.5664881', '-0.94443227', '0.66710114', '-0.02104479', '-0.62232492', '0.1199961', '0.20708077', '0.70564915', '-0.35685799', '-0.19080333']
[-0.34985686, 0.29479297, -0.27478726, 0.07474025, 0.01653126, 0.59077567, 0.82123364, -0.34997168, -0.00697975, 0.20301832, 0.73513871, 0.05697672, 0.50133399, 0.08622986, 0.09953928, 0.46035864, 0.82622047, 0.79236972, 0.63738386, 0.8267959, 0.22646075, -0.0599288, -0.06546499, -0.61105956, 0.61913797, 0.43338787, 0.84928721, -0.62307324, 0.93602742, 0.27061763, -0.01883277, 0.66671322, 0.4175206, -0.41437707, -0.19481738, -0.5664881, -0.94443227, 0.66710114, -0.02104479, -0.62232492, 0.1199961, 0.20708077, 0.70564915, -0.35685799, -0.19080333]
这篇关于Python删除字符串中指定字符,将字符串按空格切割并存储在数组中,将字符串数组转化为float型数组的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!