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
going
V2EX  ›  Python

Python 列表踢出某个元素上机作业题

  •  
  •   going · 2021-12-10 09:55:53 +08:00 · 2897 次点击
    这是一个创建于 839 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请根据输入的原始列表,输出目标列表:

    原始列表:[1,2,None,None,None,None,None,None,3,4,5,6,None,None,None,None,7,8,9,10]

    目标列表: [1,2] [3,4,5,6] [7,8,9,10]

    14 条回复    2021-12-11 04:25:50 +08:00
    princelai
        1
    princelai  
       2021-12-10 10:24:07 +08:00   ❤️ 2
    from itertools import groupby

    [list(g[1]) for g in groupby(ll,key=lambda x:x is not None) if g[0]]
    going
        2
    going  
    OP
       2021-12-10 10:34:30 +08:00
    @princelai 厉害
    learningman
        3
    learningman  
       2021-12-10 10:52:31 +08:00   ❤️ 3
    建议自己的作业自己写,楼上那哥们 pythonic 的写法你能理解吗
    ma6254
        4
    ma6254  
       2021-12-10 11:31:46 +08:00
    自己的作业自己写(滑稽
    deplivesb
        5
    deplivesb  
       2021-12-10 11:58:09 +08:00
    v2 已经沦陷为百度知道了吗
    stimw
        6
    stimw  
       2021-12-10 13:07:23 +08:00 via Android
    为什么大半年前就在问作业了...大半年后还停留在这种问题
    bytesfold
        7
    bytesfold  
       2021-12-10 13:34:04 +08:00   ❤️ 1
    @learningman 并不觉得 pythonic ,如果是公司代码不加备注基本无法维护
    JasonEWNL
        8
    JasonEWNL  
       2021-12-10 14:01:22 +08:00
    @deplivesb 高级点,Way to Overflow 。(

    @going 话说回来鉴于是作业,或可回归不依赖任何库的一般思路,日后有机会亦能写出不一定最具效率但较易维护的代码。

    ```
    il = [1, 2, None, None, None, None, None, None, 3, 4, 5, 6, None, None, None, None, 7, 8, 9, 10]
    ol = [[]]
    for i in il:
    if i:
    ol[-1].append(i)
    elif ol[-1]:
    ol.append([])
    print(ol) # [[1, 2], [3, 4, 5, 6], [7, 8, 9, 10]]
    ```
    keepeye
        9
    keepeye  
       2021-12-10 14:57:39 +08:00
    遍历一遍不就可以了吗?这题是送分题
    cnrting
        10
    cnrting  
       2021-12-10 15:06:16 +08:00 via iPhone   ❤️ 1
    print(il[0:2],il[8:12],il[16:20]) 🐶
    raycool
        11
    raycool  
       2021-12-10 15:12:59 +08:00
    @princelai 学习了,以前还真少用这个 groupby
    fml87
        12
    fml87  
       2021-12-10 15:43:42 +08:00
    lst = [1,2,None,None,None,None,None,None,3,4,5,6,None,None,None,None,7,8,9,10]

    [*map(eval,re.split(",,+",re.sub("[^0-9,]", "",str(lst) )))]
    Morii
        13
    Morii  
       2021-12-10 16:57:15 +08:00   ❤️ 1
    为啥我感觉 op 没有提问的礼貌
    szxczyc
        14
    szxczyc  
       2021-12-11 04:25:50 +08:00
    @cnrting 哈哈哈哈哈~能把我笑死
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5283 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 09:18 · PVG 17:18 · LAX 02:18 · JFK 05:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.