为啥我不能将 str 列表转换为浮点列表?

Posted

技术标签:

【中文标题】为啥我不能将 str 列表转换为浮点列表?【英文标题】:Why I can't convert a list of str to a list of floats?为什么我不能将 str 列表转换为浮点列表? 【发布时间】:2011-08-22 22:08:21 【问题描述】:

我开始写代码,但是一开始就失败了。

这是我的代码:

import csv
reader = csv.reader(open("QstartRefseqhg19.head"), dialect='excel-tab' )

for row in reader:
    C = row[1].split(",")[1:]
    C1 = [float(i) for i in C]

    print C1 

错误日志显示:

Traceback (most recent call last):
  File "/home/geparada/workspace/SJtag/src/TagGen.py", line 8, in <module>
    C1 = [float(i) for i in C]
ValueError: empty string for float()

我也试过了

import csv
reader = csv.reader(open("QstartRefseqhg19.head"), dialect='excel-tab' )

for row in reader:
    C = row[1].split(",")[1:]       
    C1 = map(float, C)        

    print C1

我的输入文件如下所示:

NM_032291   0,227,291,316,388,445,500,676,688,700,725,777,863,956,1031,1532,1660,1787,1847,1959,2115,2248,2451,2516,2681,   tttctctcagcatcttcttggtagcctgcctgtaggtgaagaagcaccagcagcatccatggcctgtcttttggcttaacacttatctcctttggctttgacagcggacggaatagacctcagcagcggcgtggtgaggacttagctgggacctggaatcgtatcctcctgtgttttttcagactccttggaaattaaggaatgcaattctgccaccatgatggaaggattgaaaaaacgtacaaggaaggcctttggaatacggaagaaagaaaaggacactgattctacaggttcaccagatagagatggaattcagcccagcccacacgaaccaccctacaatagcaaagcagagtgtgcgcgtgaaggaggaaaaaaagtttcgaagaaaagcaatggggcaccaaatggattttatgcggaaattgattgggaaagatataactcacctgagctggatgaagaaggctacagcatcagacccgaggaacccggctctaccaaaggaaagcacttttattcttcaagtgaatcggaagaagaagaagaatcacataagaaatttaatatcaagattaaaccattgcaatctaaagacattcttaagaatgctgcaactgtagatgaattgaaggcatcaataggcaacatcgcactttccccatcaccagtgaggaaaagtccgaggcgcagcccgggtgcaattaaaaggaacttatccagtgaagaagtggcaagacccaggcgttccacaccaactccagaacttataagcaaaaagcctccagatgacactacggcccttgctcctctctttggcccaccactagaatcagcttttgatgaacagaagacagaagttcttttagatcagcctgagatatggggttcaggccaaccaattaatccaagcatggagtcgccaaagttaacaaggccttttcccactggaacacctccaccactgcctccaaaaaatgtaccagctaccccaccccgaacaggatcccccttaacaattggaccaggaaatgaccagtcagccacagaggtcaaaattgaaaaactaccatccatcaatgacttggacagcatttttgggccagtattgtcccccaagtctgttgctgttaatgctgaagaaaagtgggtccatttttctgatacatccccggaacatgttactccggagttgactccaagggaaaaagtggtgtccccaccagctacaccagacaacccagctgactccccagctccaggccctctcggccccccaggtcccacaggccccccagggcctcctgggcctcctcgcaatgtactatcgccgctcaatttagaagaagtccagaagaaagtcgctgagcagaccttcattaaagatgattacttagaaacaatctcatctcctaaagattttgggttgggacaaagagcaactccacctcccccaccaccacccacctacaggactgtggtttcgtcccccggacctggctcgggccctggtccggggaccaccagtggtgcatcatcccctgctcgaccagccactcctttggttccttgcagaagtaccactccacctccacctcctccccggcctccatcccggccaaagctacctccaggaaaacctggagttggagatgtgtccagaccttttagccctcccattcattcttccagccctcctccaatagcacccttagcgcgggctgaaagcacttcttcaatatcgtcaaccaattccttgagcgcagccaccactcccacagttgagaatgaacagccttccctcgtttggtttgacagaggaaagttttatttgacttttgaaggttcttccaggggacccagccccctaaccatgggagctcaggacactctccctgttgcagcagcatttacagaaacagtcaatgcctatttcaaaggagcagacccaagcaaatgtatcgttaagattaccggagaaatggtgttgtcatttcctgctggcatcaccagacactttgccaacaacccgtccccagctgctctgacttttcgggtgataaatttcagcaggttagaacacgtcctgccaaacccccaacttctctgctgtgataatacacaaaatgatgccaataccaaggaattctgggtaaacatgccaaatttgatgactcacctaaagaaagtgtctgaacaaaaaccccaggctacatattataacgttgacatgctcaaatatcaggtgtctgcccagggcattcagtccacacctctgaacctggcagtgaattggcgatgtgagccttcaagcactgacctgcgcatagattacaaatataatacagatgcaatgacgactgctgtggccctcaacaatgtgcagttcctggtccccatcgacggaggagtcaccaagctccaggcagtgctcccaccagcagtctggaatgctgaacaacagagaatattgtggaagattcctgatatctctcagaagtcagaaaatggaggggtgggttctttgttggcaagatttcagttatctgaaggcccaagcaaaccttctccattggttgtgcagttcacaagtgaaggaagcaccctttctggctgtgacattgaacttgttggagcagggtatcgattttcactcatcaagaaaaggtttgctgcaggaaaatacttggcagataactaatgaaatcttatgcaaggatttggaggattcatataatggagaactgatgtatgagaaacagattttaattttggtttgatgaaaacaaaccaatatctgcacttgggatatatcaggtggaaagtcaatgactttcatctgtgatttccctcacacactaccatgatgaccagtcctacagtatttacttctaggtgtaatattgttaatggttttaaaatgtaattattgtatttgtaaattgtactctcattccagtaaggcagttagacacttgagttttagcattttaccattcctgaaatggatgtaatttaaactgtggtatgtaaatttaatagtagtattgttgaatggcacaatgcttacagaggtagattgcattttgtcaatatataaaatttaaatataatattgatagctgtcataaagggggtgccacatattaaagaaacttaagtggaaccagaagaaaaagaaacaaacttacttttcttcaatgcttagtatgttttactctagtgctaaataaaaactctatcttcaaatgtttagtgggttaaattgagaaactatttcagaaaaaaattctaaggttacagcatattcaaagaaaagcattagttaccactttttaaaaagcttttttttcaaactgcaaatttcataaaaatgcaaactgtgtaaacagggcctcttatttttataacttgtgtaaaaagggaaagcaattcatatttaaagtttaagtatattaaattataatcaagagtaaagaagatgttgaagtcttaactacttgcccctctctacagtttcgcaaatgtggggattgctgaataatcagtcagactaaaaccaaaattgtgattttaagatttcaagactttccgtagttgaactggttaagaatttttgcttagttactctgaatagatgatcttactcatccagtatgggggaatgatacctcacgtcttcctctttacccacaggaatcaaaacgctgagactgagaattttagggaaaaaaaagtccactgtttagatccagaaggagagttttaatcattgtttatatcatttgagaatgaaaaaataagcttcataaatgaaattctattcacattactgtgtaataaatttccttttggatgattaggattcattgtataaaactgtaaatctttgccattcttggagaagcaaaaggagagttatcaaaaatgtatgtcgtttcatcgttgcaaggtataataaaaactgtaattattcaatctggccctgccatatgaacatttagaaagacaaacttcttcgggagtctcagttgtaaaaccttccctcattaatatctgaaaatgttagtcttcctttaagtcatagaacttatttaaacataaaccaatttctattacaggttatgctattaaatagctgtaattattaagttattatttttataattagttgttaaatttcattttacacccactcaaatttaacaaagaatctttagcccctttaaattttagaattaaattaaatttttaaagttttacttctaaaatgagattgtgactggcaattgtttatagtgaaactttttaaattaatctttgtactcctctatcagtgcttgctaccaagagaatgtccaaaatgatttgttttaccatgggaaaattcttactattcaacaaactctcagttggccccctacagcagtctggtgttgaagtttctttgaacgaactaaatatactcattttatgtaaaggtatccaatttgattttgaaaccaaaatagaaaatgcaaaattctaaattccatgaaacatggaatttatgacaccaaaatcaatggagagtaagcagcagcaaactgagaattatccagcatatgaatataacaatgtgtttttaagtaatcaattcatttaaaaaattgaatattaatacaaagcatattaaaaacatgtaaatatta
NM_001080397    0,397,490,715,1443,1597,1774,1980,  atgatccccgcagccagcagcaccccgccgggagatgccctcttccccagcgtggccccacaggacttctggaggtcccaggtcacgggctactcggggtccgtgacacgacacctcagtcaccgggccaacaacttcaaacgacaccccaagaggaggaagtgcattcgtccctccccacccccgccccccaacaccccgtgcccgcttgagctggtggacttcggggacctgcacccccagaggtccttccgggagctgcttttcaacggctgcattctctttggcatcgagttcagctacgccatggagacggcgtacgtgaccccggtgctcctgcagatgggcctgcccgaccagctctacagcctggtgtggttcatcagccccatcctcggattcctactgcagcctctgttgggtgcttggagtgaccggtgtacctcaaggtttggaaggagacgccctttcattcttgtcctggctataggggcactgctgggcctctcgctcttgctgaatggccgggacattggcatcgccctggctgacgtgaccgggaaccacaagtggggcctgctgctgaccgtgtgcggtgtggtgctgatggactttagcgccgactcggcggacaaccccagccacgcctacatgatggacgtgtgcagccccgcagaccaggaccgaggcctgaacatccacgccctcctggcaggtctcggaggaggctttggatacgtggtcggcggaatccactgggataaaacgggcttcgggagggccctggggggacagctccgagtcatttacctcttcactgcggtcaccctgagcgtcaccaccgtcctgaccctggtcagcatccctgagaggccgctgcggccgccgagtgagaagcgggcagccatgaagagccccagcctcccgctgcccccgtccccacccgtcctgccagaggaaggccctggcgacagcctcccgtcgcacacggccaccaacttctccagccccatctcgccgcccagccccctcacgcccaagtacggcagcttcatcagcagggacagctccctgacgggcatcagcgagttcgcctcatcctttggcacggccaacatagacagcgtcctcattgactgcttcacgggcggccacgacagctacctggccatccctggcagcgtccccaggccgcccatcagcgtcagcttcccccgggcccccgacggcttctaccgccaggaccgtggacttctggagggcagagagggtgccctgacctccggctgtgacggggacattctgagggtgggctccttggacacctctaagccgaggtcatcagggattctgaagagacctcagaccttggccatcccggacgcagccggaggagggggtcccgaaaccagcaggagaaggaatgtgaccttcagtcagcaggtggccaatatcctgctcaacggcgtgaagtatgagagcgagctgacgggctccagcgagcgcgcggagcagcctctgtccgtggggcgcctctgctccaccatctgcaacatgcccaaggcgctacgcaccctctgcgtcaaccacttcctggggtggctctcattcgaggggatgttgctcttctacacagacttcatgggcgaggtggtgtttcagggggaccccaaggccccgcacacatcagaggcgtatcagaagtacaacagcggcgtgaccatgggctgctggggcatgtgtatctacgccttcagtgctgccttctactcagctatcctggagaagctggaggagttcctcagcgtccgcaccctctacttcatcgcctatctcgccttcggcctggggaccgggcttgccaccctctccaggaacctctacgtggtcctgtcgctctgcataacctacgggattttattttccaccctgtgcaccttgccttactcgctgctctgcgattactatcagagtaagaagtttgcagggtccagtgcggacggcacccggcggggcatgggcgtggacatctctctgctgagctgccagtacttcctggctcagattctggtctccctggtcctggggcccctgacctcggccgtgggcagtgccaacggggtgatgtacttctccagcctcgtgtccttcctgggctgcctgtactcctccctgtttgtcatttatgaaattcctcccagcgacgctgcagacgaggagcaccggcccctcctgctgaacgtctgacatcgcggagcctcgactccggacacgcgcctgcacctgggggtctggagcaggccgaccagtgaggaccaaagggccttgttggacagggggactggctgcctactggaatgtaaatatgtgataaaataataaatgacagcggcaaagccta
NM_001145277    0,182,283,388,470,579,757,  gaaacctggtcagagagtcgcaccgcttccgtccgtcggacagaggaacggtggaagtcgccggaagttcggtgggctccaggcgtcgcgatggaggagagcgggtacgagtcggtgctctgtgtcaagcctgacgtccacgtctaccgcatccctccgcgggctaccaaccgtggctacagggctgcggagtggcagctggaccagccatcatggagtggccggctgaggatcactgcaaagggacagatggcctacatcaagctggaggacaggacgtcaggggagctctttgctcaggccccggtggatcagtttcctggcacagctgtggagagtgtgacggattccagcaggtacttcgtgatccgcatcgaagatggaaatgggcgacgggcgtttattggaattggcttcggggaccgaggtgatgcctttgacttcaatgttgcattgcaggaccatttcaagtgggtgaaacagcagtgtgaatttgcaaaacaagcccagaacccagaccaaggccctaaactggacctgggcttcaaggagggccagaccatcaagctcaacatcgcaaacatgaagaagaaggaaggagcagctgggaatccccgagtccggcctgccagcacaggagggctgagcctgcttccccctcccccaggggggaaaacctccaccctgatccctccccctggggagcagttggctgtggggggatccctcgtccagccagcagttgctcccagttcagatcaacttccagccagacccagccaggcacaggctgggtccagttctgacctgagcacggtttttcctcatgtgacttctgggaaggcgctccctcatctgggccaaaggaaggaggacgaagccctcctcagctggcctgtgtttggggcatgaatctctcctctcctccttgtctggctctgttgacaaaccgggcatgtttggcagtaaattggcaccgtgtcacactgtttcctgggattcaagtatgcaaccagaacacaggagaagaaaagctccaggatccctgtccccatctgtcctcttgatgtgagagagactctgagacttcttccatcgcaatgacctgtattaaacacaagccccccaagcaaaagaagaggttgagtttgctgccaggattcagatcagcccttcccagggtctgcaggtgtcacatgatcacagttcagcgggaggctttccgtacccacactggctgtagccacttcagtccatctgccctccagaggaggggtttcttcctgatttttagcaggtttagaggctgcagcttgagctacaatcaggagggaaattggaaggattagcagcttttaaaaatgtttaaatattttgctttgctaatgtgctgatccgcactaactcatctttgcaaaaggaactgctccctcggcgtgccccagctggggcctctgaagggattcctcactgtgggcagctgccctgagcttcaggcagcagtgtttatctctggccagttgtctggtttccatgtattctaggccaggtaggcaacacagagccaaggcgggtgctggaagccagacggaacagtgttggggcaggaaggtggatgctgttgtcatggagctgtgggagttggcactctgtctgctggtggccctctcggctcacatgttcacagtgcagctcctggcagacttgggttttctctttggtggtttctaaagtgccttatctgcaaacaacttcttttctccttcaggaactgtgaatggctagaagaaggagctcagtaaactagaagtccagggttgcttggtttactggtttataagaaatctgaaagcacctctgacattccttttattaactcacctctcagttgaaagatttcttctttgaaaggtcaagaccgtgaactgaaaaaagtgttggcctttttgcgggaccagatttttaagataaaataaatatttttacttctgtcattgtatgtgaaaaaaaaaaaaa

我是堆栈,感谢您的时间!!

【问题讨论】:

调试提示:在失败语句之前,做print "C:", C 【参考方案1】:

您的输入行以逗号结尾,例如:

0,182,283,388,470,579,757,

所以很自然,这将被拆分为:

['0', '182', '283', '388', '470', '579', '757', '']

最后一个元素总是一个空字符串''。你需要考虑到这一点。一种方法是简单地忽略它:

C1 = [float(i) for i in C if i]

或者通过在转换之前删除最后一个元素:

C = row[1].split(",")[1:-1]   # the slice will exclude first and last elements

【讨论】:

也可以先使用strip()方法:C = row[1].strip(',').split(',')[1:] @Blair:我不得不这么说。感谢您指出了这一点。昨天我的心似乎在某个时候崩溃了。我希望它现在已修复。【参考方案2】:

您可以在转换为浮点数并捕获 ValueErrors 时使用 try/except。

C = row[1].split(",")[1:] 
    for item in C:
    try:
        convert=float(item)
    except ValueError:
        print "not a number"

【讨论】:

以上是关于为啥我不能将 str 列表转换为浮点列表?的主要内容,如果未能解决你的问题,请参考以下文章

如何将字符串列表转换为c#中的浮点列表,统一?

将字符串浮点数列表转换为具有 2 个小数点的浮点数列表

将Python3 List(带str参数)转换为Bytes。

从浮点字符串到 Int 的 Python 类型转换在创建列表时无法在 for 循环中工作

将 PyTorch 张量转换为 python 列表

将字符串转换为列表 - str2list('[abc]') 应该返回 ['a','b','c']