Python 单列表等分为多列表小问题

2022-06-19 15:16:10 +08:00
 leorealman

代码如下

def test():
    a = []
    for i in xrange(1,126):
        ii = tuple(['hello',i])
        a.append(ii)
        if len(a) == 10:
            print a
            del a[0:]
        else:
            continue

test()

执行结果如下

[root@xxxx wodee]# python aa.py
[('hello', 1), ('hello', 2), ('hello', 3), ('hello', 4), ('hello', 5), ('hello', 6), ('hello', 7), ('hello', 8), ('hello', 9), ('hello', 10)]
[('hello', 11), ('hello', 12), ('hello', 13), ('hello', 14), ('hello', 15), ('hello', 16), ('hello', 17), ('hello', 18), ('hello', 19), ('hello', 20)]
[('hello', 21), ('hello', 22), ('hello', 23), ('hello', 24), ('hello', 25), ('hello', 26), ('hello', 27), ('hello', 28), ('hello', 29), ('hello', 30)]
[('hello', 31), ('hello', 32), ('hello', 33), ('hello', 34), ('hello', 35), ('hello', 36), ('hello', 37), ('hello', 38), ('hello', 39), ('hello', 40)]
[('hello', 41), ('hello', 42), ('hello', 43), ('hello', 44), ('hello', 45), ('hello', 46), ('hello', 47), ('hello', 48), ('hello', 49), ('hello', 50)]
[('hello', 51), ('hello', 52), ('hello', 53), ('hello', 54), ('hello', 55), ('hello', 56), ('hello', 57), ('hello', 58), ('hello', 59), ('hello', 60)]
[('hello', 61), ('hello', 62), ('hello', 63), ('hello', 64), ('hello', 65), ('hello', 66), ('hello', 67), ('hello', 68), ('hello', 69), ('hello', 70)]
[('hello', 71), ('hello', 72), ('hello', 73), ('hello', 74), ('hello', 75), ('hello', 76), ('hello', 77), ('hello', 78), ('hello', 79), ('hello', 80)]
[('hello', 81), ('hello', 82), ('hello', 83), ('hello', 84), ('hello', 85), ('hello', 86), ('hello', 87), ('hello', 88), ('hello', 89), ('hello', 90)]
[('hello', 91), ('hello', 92), ('hello', 93), ('hello', 94), ('hello', 95), ('hello', 96), ('hello', 97), ('hello', 98), ('hello', 99), ('hello', 100)]
[('hello', 101), ('hello', 102), ('hello', 103), ('hello', 104), ('hello', 105), ('hello', 106), ('hello', 107), ('hello', 108), ('hello', 109), ('hello', 110)]
[('hello', 111), ('hello', 112), ('hello', 113), ('hello', 114), ('hello', 115), ('hello', 116), ('hello', 117), ('hello', 118), ('hello', 119), ('hello', 120)]

问题如下

如果能保证最后一个长度不够的列表也能够打印出来?基于上面的代码改造

2229 次点击
所在节点    Python
15 条回复
leorealman
2022-06-19 15:17:49 +08:00
请路过的大佬指点一二,多谢了
liprais
2022-06-19 15:25:27 +08:00
第一个 for 下面写 else
irisdev
2022-06-19 15:25:37 +08:00
不写 python
if len(a) == 10 => if len(a) == 10 or i == 126 - 1
ranleng
2022-06-19 15:28:50 +08:00
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]

def test():
arr = [("hello world", i) for i in range(126)]
for i in chunks(arr, 10):
print(i)

test()
leorealman
2022-06-19 15:30:55 +08:00
@irisdev 还有更好的方法嘛?我不太希望使用 126 - 1 这个来判断这是最后一轮循环
Muniesa
2022-06-19 15:34:32 +08:00
python2 代码?没学过 python2 ,如果没理解错的话 python3 直接 for 结束后 print 就可以啊。
DGideas
2022-06-19 15:45:15 +08:00
```
a = [("hello", i) for i in range(126)]
print(a)

while a:
print(a[:10])
a = a[10:]
```
上边这样就行了,Python2 的话,记得把 range 改成 xrange
DGideas
2022-06-19 15:46:33 +08:00
格式乱了,我再贴一下图片吧

lianjin
2022-06-19 16:00:28 +08:00
@ranleng GREAT
lianjin
2022-06-19 16:02:25 +08:00
其实你的逻辑很简单。
就是少了个判断,判断下数组里是否 len() >1 。可以自己实现。
也可以用 yield 来帮你实现,如 @ranleng 写的
ClericPy
2022-06-19 17:28:18 +08:00
参考 slice_into_pieces slice_by_size

https://github.com/ClericPy/torequests/blob/master/torequests/utils.py#L397

试过其他几种, 这个速度最快, 原理想不起来了, 从老外那抄的
UrsulaTucker
2022-06-19 19:09:59 +08:00
leorealman
2022-06-19 19:31:40 +08:00
多谢各位大佬指点
zvDC
2022-06-19 19:49:26 +08:00
xs = [[('hello', x * 10 + y) for y in range(1, 11)] for x in range(12)]
infun
2022-06-19 20:33:02 +08:00
```python
def slice_list(raw_list, size):
slice_num = len(raw_list) // size
if len(raw_list) % size > 0:
slice_num += 1
sliced_list = [raw_list[(i-1)*size : i*size] for i in range(1, slice_num+1)]
return sliced_list

if __name__ == "__main__":
print(slice_list(list(range(1,101)), 19))
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/860658

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX