【问题标题】:Converting Multiple string inputs to int将多个字符串输入转换为 int
【发布时间】:2021-11-03 14:06:19
【问题描述】:

我正在尝试将abcd 转换为整数,但在我尝试这样做之后,它们仍然以字符串的形式出现。我尝试使用循环而不是 map,但这也不起作用。

inputs = input()
split_input = inputs.split()
a, b, c, d = split_input
split_input = list(map(int, split_input))

【问题讨论】:

  • 您的abcd 变量被分配了split_input 数组的结果,该数组在此分配时是一个字符串数组正在发生。移动您的地图操作首先发生,以便您从 that 的结果分配而不是

标签: python-3.x list


【解决方案1】:

只需交换最后两行:

split_input = list(map(int, split_input))
a, b, c, d = split_input

除非你以后需要split_input,否则你根本不需要列表转换:

split_input = map(int, split_input)
a, b, c, d = split_input
# OR in fact simply
a, b, c, d = map(int, split_input)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多