【问题标题】:why to use `list(map(str,input().split()))` when `input().split()` is already list当 `input().split()` 已经是列表时,为什么要使用 `list(map(str,input().split()))`
【发布时间】:2020-03-30 15:05:46
【问题描述】:

n 这里得到一个句子的输入,比如“嗨,我是初学者”

n=list(map(str,input().split()))

input().split()

我需要把它转换成字符串列表 两者都有效,但是...

有什么区别?

这里不需要 list(map()) 吗?

【问题讨论】:

    标签: python-3.x string list input


    【解决方案1】:

    input() 总是返回一个字符串。

    所以map(str,input().split()) 是多余的,相当于input().split()

    参考:input()

    【讨论】:

      【解决方案2】:

      这两种方式是等价的,你只需要写第二种方式。但是如果你需要一个int类型或者其他类型的列表,你可以这样写:

      n = list(map(int, input().split()))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-12
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 2019-08-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多