首页 > 解决方案 > python3中的split()没有按预期返回值(拆分整个字符串字符而不是空格)

问题描述

size = int(input())
#a = [0 for x in range(size)]
a = input()
b=[]
a.split()
print (a[0])
for x in a:
    if is_number(x):
        b.append(int(x))
print (b)

输入:4

12 8 4 0

输出:1

[1, 2, 8, 4, 0]

期待:12

[12、8、4、0]

我在这里做错了什么?注意:a.split(" ")也不工作

标签: pythonpython-3.x

解决方案


split 不会就地改变字符串,它会返回。所以,只需执行以下操作

a = a.split()

推荐阅读