Python 单列表等分为多列表小问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
leorealman
V2EX    Python

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

  •  
  •   leorealman 2022-06-19 15:16:10 +08:00 3108 次点击
    这是一个创建于 1262 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下

    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)] 

    问题如下

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

    15 条回复    2022-06-19 20:33:02 +08:00
    leorealman
        1
    leorealman  
    OP
       2022-06-19 15:17:49 +08:00
    请路过的大佬指点一二,多谢了
    irisdev
        3
    irisdev  
       2022-06-19 15:25:37 +08:00
    不写 python
    if len(a) == 10 => if len(a) == 10 or i == 126 - 1
    ranleng
        4
    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
        5
    leorealman  
    OP
       2022-06-19 15:30:55 +08:00
    @irisdev 还有更好的方法嘛?我不太希望使用 126 - 1 这个来判断这是最后一轮循环
    Muniesa
        6
    Muniesa  
       2022-06-19 15:34:32 +08:00
    python2 代码?没学过 python2 ,如果没理解错的话 python3 直接 for 结束后 print 就可以啊。
    DGideas
        7
    DGideas  
       2022-06-19 15:45:15 +08:00   1
    ```
    a = [("hello", i) for i in range(126)]
    print(a)

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

    lianjin
        9
    lianjin  
       2022-06-19 16:00:28 +08:00
    @ranleng GREAT
    lianjin
        10
    lianjin  
       2022-06-19 16:02:25 +08:00
    其实你的逻辑很简单。
    就是少了个判断,判断下数组里是否 len() >1 。可以自己实现。
    也可以用 yield 来帮你实现,如 @ranleng 写的
    ClericPy
        11
    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

    试过其他几种, 这个速度最快, 原理想不起来了, 从老外那抄的
    leorealman
        13
    leorealman  
    OP
       2022-06-19 19:31:40 +08:00
    多谢各位大佬指点
    zvDC
        14
    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)]
    GlobalNPC
        15
    GlobalNPC  
       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))
    ```
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     4382 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 04:03 PVG 12:03 LAX 20:03 JFK 23:03
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86