python - 将整数添加到列表中,同时将由空格分隔的数字字符串转换为整数
问题描述
我目前正在尝试附加整数和字符串,我正在尝试将其转换为整数,因为它们只能是用空格分隔的数字。我当前的代码:
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']]
解决方案
代替
Data.append(added)
和
for d in added:
Data.append([d])
然后
1
22
2 3 4
[['1'], ['22'], ['2'], ['3'], ['4']]
推荐阅读
- javascript - Puppeteer 视口设置在创建 PDF 时无效
- angular - Angular - 无法以两种方式进行操作
- google-sheets - 如何根据谷歌表格中的星期几对列中的值求和
- excel - 使用 VLookup 函数查找单元格以更改其值
- angular - 如何在 Azure DevOps Pipelines 上实例化后端?
- angular - Ionic 4 - 离子选择不会滚动到警报中的选定项目
- python - 如何在 Pandas 中用多个唯一字符串替换重复值?
- powershell - 将自定义行添加到 $String | 选择 | 格式表输出请求
- java - 如何使用 Hibernate 会话批量执行 SQL 查询
- javascript - Gulp - 如果 CSS 大小大于 50 kb,则 gulp 进程失败