【问题标题】:Changing one element in a list from string to integer将列表中的一个元素从字符串更改为整数
【发布时间】:2018-06-20 05:38:23
【问题描述】:

我正在查看此post,但我无权添加评论。

我的问题是:

> results = [int(i) for i in results]

>>> results = ["1", "2", "3"]

>>> results = [int(i) for i in results]

>>> results

[1, 2, 3]

如果我只需要更改列表中的第一个元素,我该怎么做? 通过索引?就像这样:

> results=[int(1) for i in results]

【问题讨论】:

    标签: python string list int


    【解决方案1】:

    只需简单地写:

    results[1] = int(results[1])
    

    改变第一个元素:

    results[0] = int(results[0]) #because of 0 based indexing
    

    一般:

    results[index] = int(results[index])
    

    【讨论】:

    • 它说我可以在 7 分钟内接受答案。我只是在等那 7 分钟
    • 接受!谢谢!
    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    相关资源
    最近更新 更多