【问题标题】:How to take input as integers into a list which is seperated by space in python? [duplicate]如何将输入作为整数输入到python中用空格分隔的列表中? [复制]
【发布时间】:2021-12-07 03:17:18
【问题描述】:

我尝试使用下面的代码,但列表包含整数作为字符

>>>a=input().split()
2 3 4
>>>print(a)
['2','3','4']

【问题讨论】:

    标签: python


    【解决方案1】:
    >>> a=list(map(int,input().split()))
    1 2 3
    >>> print(a)
    [1, 2, 3]
    

    尝试使用此代码。 这里使用了 map() 函数,它返回一个地图对象,我们可以使用 list 将输入存储在一个列表中

    【讨论】:

      【解决方案2】:
      a = [int(x) for x in input().split()]
      

      1 2 3 的输出:

      [1, 2, 3]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-04
        • 2020-06-17
        • 2022-08-11
        • 1970-01-01
        • 2022-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多