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

Python 的[*range(17), *range(26, 16, -1)] 是什么操作?

  •  
  •   fyooo · 2020-01-20 17:53:21 +08:00 · 1123 次点击
    这是一个创建于 1548 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这里 看到的例子 :

    outline = landmarks[[*range(17), *range(26,16,-1)]]
    

    看不懂其中的数组操作符 *range ,有同行可以给个提示么?

    6 条回复    2020-01-24 13:49:01 +08:00
    DGideas
        2
    DGideas  
       2020-01-20 17:56:23 +08:00   ❤️ 1
    在你的例子中,即将两个 range()生成的序列(两个 list 对象)解包,并合并为一个 list
    Takamine
        3
    Takamine  
       2020-01-20 18:20:18 +08:00 via Android   ❤️ 1
    解包。~
    hbl
        4
    hbl  
       2020-01-20 19:42:06 +08:00 via Android   ❤️ 1
    一个星号是解列表。两个星号是解字典。
    jdhao
        5
    jdhao  
       2020-01-20 19:44:52 +08:00   ❤️ 1
    叫做 unpacking,如楼上所说,一个 * 是把列表的元素拿出来。
    di94sh
        6
    di94sh  
       2020-01-24 13:49:01 +08:00 via Android
    []和 list 是等价的[*args,*args_2]和 list(*args,*args_2)是等价的,参考 Python 星号收集和展开
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1018 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 22:24 · PVG 06:24 · LAX 15:24 · JFK 18:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.