python-3.x - 如何将列表拆分为每个最小大小的相等块?
问题描述
这将追加 0、1、2,然后是 3、4、5,然后是 6、7、8。虽然我需要它来追加 0、1、2,然后是 3、4、5,然后是 6、7、8、9。我需要它将列表的全部内容考虑到最后一组。这可能吗(知道我会考虑这些数字以进行进一步的操作)?
lst = [0,1,2,3,4,5,6,7,8,9]
templist=[]
ctr1=0
for i in range (int (len(lst)/3)):
for n in lst[ctr1:3+ ctr1]:
templist.append(n)
ctr1+=3
print (templist)
解决方案
lst = [0,1,2,3,4,5,6,7,8,9]
templist=[]
ctr1=0
x = len(lst)
print ("x",x)
for i in range (int (len(lst)/3)):
for n in lst[ctr1:3+ ctr1]:
print ("hello")
templist.append(n)
#print (templist)
print (x)
y= x-3
print ("y",y)
if y<3 and y % 3:
templist.extend(lst[ctr1:])
print ("yes")
print (templist)
#for n in lst[ctr1:3+ ctr1]:
#templist.append(n)
ctr1+=3
x=x-3
print (templist)
推荐阅读
- react-native - 如果我在函数中间更新状态,函数其余部分的执行顺序是什么?
- batch-file - 如何在没有空格的情况下批量计算句子中的每个字母
- powershell - 从服务器获取奇怪的错误 - PowerShell
- flutter - Flutter Navigator.of(context).pop() in showDialog, close full app in ios
- python - 无法将符号张量 (sequential/lstm/strided_slice:0) 转换为 numpy 数组。在 MacBook Pro M1 上使用 TensorFlow 时出现问题
- python - Python 中的 switch-case 语句
- html - HTML 和 CSS 图像不在盒子内
- azure-devops - 使用打包生成和发布配置文件的 Azure DevOps 发布管道
- python - 如何在 Python 中将用户输入格式化为变量
- odoo - Odoo - 不按 _rec_name 过滤 Many2one