python输入列表(数字列表)

使用方法
  • split()
  • map()
  • input()
split()

参数
-str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
-num – 分割次数。默认为 -1, 即分隔所有。

s = input("输入:").split()
print(s)

运行

输入:1 2 3 4
['1', '2', '3', '4']
map()

map()会根据提供的函数对指定序列做映射。
map(function, iterable, …)

s = input("输入:").split()
print(s)
print(map(int,s))

运行

输入:1 2 3
['1', '2', '3']
<map object at 0x0000022C58D8B4F0>

python3 中 map返回值不是列表而是iterators,可以用用list方法转换成列表

最后输入列表即可用

array = list(map(int,input("输入用空格分隔的数字:").split())) 
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐