python 设计一个西数dimension_ array0,输入若干个整数,把这些整数按顺序封 装到一个二维列表中,要求每个子列表包含3个元素
时间: 2024-05-04 19:18:27 浏览: 187
可以使用以下代码实现:
```python
import math
class DimensionArray:
def __init__(self):
self.array = []
def add(self, num):
if len(self.array) == 0 or len(self.array[-1]) >= 3:
self.array.append([])
self.array[-1].append(num)
def get_array(self):
if len(self.array) == 0 or len(self.array[-1]) == 3:
return self.array
else:
# 如果最后一个子列表不足3个元素,则补齐空位
missing_num = 3 - len(self.array[-1])
for i in range(missing_num):
self.array[-1].append(0)
return self.array
dimension_array = DimensionArray()
# 输入一串整数,每个整数使用空格分隔
num_str = input("请输入若干个整数:")
nums = map(int, num_str.split())
for num in nums:
dimension_array.add(num)
# 输出结果
print(dimension_array.get_array())
```
运行示例:
```
请输入若干个整数:1 2 3 4 5 6 7
[[1, 2, 3], [4, 5, 6], [7, 0, 0]]
```
阅读全文
相关推荐

















