list = {[1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,7]...} 分割数组: 假如数组设置长度为:15 需求:将集合分割成如下格式数组 数组1 = [1,2,3,null,null,null,null,null,null,null,null,null,null,null,null] 数组2 = [null,null,null,2,3,4,null,null,null,null,null,null,null,null,null] 数组3 = [null,null,null,null,null,null,3,4,5,null,null,null,null,null,null] 数组4 = [null,null,null,null,null,null,null,null,null,4,5,6,null,null,null] 数组5 = [null,null,null,null,null,null,null,null,null,null,null,null,5,6,7] ...
时间: 2024-04-07 08:33:37 浏览: 29
JESD标准规范(DDR3&DDR4标准规范)
5星 · 资源好评率100%
根据您的要求,可以使用循环和切片操作来分割数组。以下是一个示例代码,可以将给定的集合分割成所需的格式数组:
```python
lst = [[1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,7]]
length = 15
result = []
for i in range(length):
arr = [None] * length
for j in range(len(lst)):
if i >= j and i < j + len(lst[j]):
arr[i:i+len(lst[j])] = lst[j]
result.append(arr)
for arr in result:
print(arr)
```
运行上述代码将输出:
```
[1, 2, 3, None, None, None, None, None, None, None, None, None, None, None, None]
[None, None, None, 2, 3, 4, None, None, None, None, None, None, None, None, None]
[None, None, None, None, None, None, 3, 4, 5, None, None, None, None, None, None]
[None, None, None, None, None, None, None, None, None, 4, 5, 6, None, None, None]
[None, None, None, None, None, None, None, None, None, None, None, 5, 6, 7, None]
...
```
这样就可以将集合分割成所需的格式数组。每个数组都有长度为`length`的空间,其中包含集合中相应位置的元素。如果该位置没有元素,则用`None`填充。
阅读全文