python - 如何在python中将整数动态二维数组作为输入(在线IDE)
问题描述
我想在未定义行数和列数的在线编译器中将一个自由大小的二维数组作为用户在 python 中的输入。
For example if the input is :
1 2 3 4
5 6 7
8 9
10
11 12 13 14 15
I want to store it like [[1,2,3,4],[5,6,7],[8,9],[10],[11,12,13,14,15]]
解决方案
您应该知道,当代码中有过多的输入语句但没有输入考虑到这一点时,在线 IDE 会抛出 EOF 错误我们可以在在线 IDE 中进行动态输入。
matrix = []
while True:
try:
matrix.append(list(map(int,input().split())))
except:
break
您可以通过在每个新行的末尾也知道进位返回来做到这一点,除了最后一行之外,您将有进位返回 ('\r')
matrix = []
while True:
row = input()
matrix.append(row.strip())
print(row)
if row[-1] != '\r':
break
推荐阅读
- django - 如何在 django-yasg 上添加前缀“token”?
- python - Kivy 问题根据其他微调器选择更新微调器值(导航文件目录)
- python - Opencv:在FFT上检测最亮的线
- python - 尝试安装 Flask - 错误:root:找不到哈希 md5 的代码
- java - 动态添加行到表格 - Easytable (PDFBox)
- python - 在“for”循环中列出附加问题
- python-3.x - Pandas:按数据框行索引的前两个字符分组
- qt - Qt Multi-Thread Queued Connection Signal / Slot 问题(SLOT 不触发)
- html - 容器上的 Flex 方向和 flex wrap 会在 flex-item 中产生额外的空间
- stata - 如何在Stata中重新编码范围内的缺失值