【问题标题】:How to remove apostrophe from List in Python [duplicate]如何从Python中的List中删除撇号[重复]
【发布时间】:2019-03-14 11:55:14
【问题描述】:

我正在使用 Python 开发机器学习程序,但下面的代码行有问题

 [['23,24,-23,12,32,54,64,12,4,0,13,10']] 

我想使用 LinearDiscriminantAnalysis 转换此数据,但我希望删除撇号。 我希望我的输出是:

[[23,24,-23,12,32,54,64,12,4,0,13,10]] 

我不知道 [ ] 之前和之后的 '。 感谢期待。

【问题讨论】:

  • 删除外部引号,如重复问题所示。这需要split 操作。这将为您留下一个字符串列表:["23", "24", ...]。将这些项目中的每一项转换为int,您就会拥有所需的列表。
  • [[int(x) for x in '23,24,-23,12,32,54,64,12,4,0,13,10'.split(',') if x.strip()]] 试试这个

标签: python python-3.x machine-learning spyder


【解决方案1】:
In[2]: a = [['23,24,-23,12,32,54,64,12,4,0,13,10']]
In[3]: result = []
In[4]: for sublist in a:
  ...:     for elem in sublist:
  ...:         result.append([int(b) for b in elem.split(',')])
  ...: 
In[5]: result
Out[5]: [[23, 24, -23, 12, 32, 54, 64, 12, 4, 0, 13, 10]]

【讨论】:

    猜你喜欢
    • 2020-11-15
    • 1970-01-01
    • 2021-12-17
    • 2014-09-12
    • 2016-07-02
    • 1970-01-01
    • 2011-04-19
    • 2015-05-23
    • 1970-01-01
    相关资源
    最近更新 更多