【问题标题】:How can I turn the items within a list that is within another list, turn them from char to ints ( remove the '' from them), in python?如何在 python 中将列表中的项目转换为另一个列表中的项目,将它们从 char 转换为 int(从中删除 \'\' ?
【发布时间】:2022-12-17 21:40:58
【问题描述】:

[['1', '1'], ['1', '2'], ['1', '3'], ['1', '4'], ['2', '1'] , ['2', '2'], ['2', '5'], ['3', '3'], ['3', '4'], ['3', '6'] , ['4', '5'], ['5', '6']]

这就是我得到的,但我正在尝试返回

[[1,1], [1,2], [1,3], [1,4] 等

**my code to take each line of the file**

##获取文本文件的每一行 filename = input("请输入文件名:") 打开(文件名)作为 file_in: 行 = [] 对于 file_in 中的行: 单词 = line.split() lines.append(单词)


【问题讨论】:

    标签: python arrays string integer filereader


    【解决方案1】:

    遍历每个“单词”并使用类似 int(word) 的方式将它们转换为 int。请注意,如果字符串未格式化为 int,他可能会抛出异常,因此包含 try/catch 是明智的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2022-01-10
      • 2012-07-21
      • 1970-01-01
      • 2012-12-15
      • 2015-12-31
      • 2012-02-05
      相关资源
      最近更新 更多