【发布时间】:2021-03-13 08:40:31
【问题描述】:
我目前正在尝试追加整数和字符串,我正在尝试将其转换为整数,因为它们只能是用空格分隔的数字。 我当前的代码:
def check(x):
if type(x) == str:
x = x.split()
return x
else:
return x
Data = []
while True:
try:
numbers = input()
if numbers !='':
added = check(numbers)
Data.append(added)
else:
print(Data)
break
except EOFError as error:
print(Data)
break
但这并不完全符合我的需要。 例如
的输入 1
22
1 2 3
给我输出
[['1'], ['22'], ['2', '3', '4']]
虽然我想要输出
[['1'], ['22'], ['2'], ['3'], ['4']]
【问题讨论】:
-
欢迎来到 StackOverflow。您的示例输入与您想要的输出不匹配。 (输入为 1 2 3,但输出为 2 3 4。)请您编辑您的问题吗?
标签: python python-3.x