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 · 2180 次点击
    这是一个创建于 648 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下

    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
    请路过的大佬指点一二,多谢了
    liprais
        2
    liprais  
       2022-06-19 15:25:27 +08:00 via iPhone
    第一个 for 下面写 else
    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)]
    infun
        15
    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))
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3205 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 11:35 · PVG 19:35 · LAX 04:35 · JFK 07:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.