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

问个列表转化的小问题

  •  
  •   JCZ2MkKb5S8ZX9pq · 2019-10-15 21:17:37 +08:00 · 1691 次点击
    这是一个创建于 1655 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def test(pos,target):
    	x, y = pos
    	color = img[x, y]
        ....
    
    • 请问上面这两步有办法简写嘛?
    • 假设 pos 很长,都要拆开写入 img[]内,的情况下,类似*参数的写法有嘛?
    inhzus
        1
    inhzus  
       2019-10-15 21:51:17 +08:00
    如果没有理解错的话,pos 类型为 tuple,img[x, y] 调用的内置函数为 __getitem__,那就直接 img[pos] 就好了。
    如图 ![image.png]( https://i.loli.net/2019/10/15/gTp1e3W8oaDAdvu.png)
    inhzus
        2
    inhzus  
       2019-10-15 21:53:04 +08:00   ❤️ 1
    @inhzus #1 上一张复制的有些问题,见这张 ![image.png]( https://i.loli.net/2019/10/15/RkbDN6miOwptyzH.png)
    JCZ2MkKb5S8ZX9pq
        3
    JCZ2MkKb5S8ZX9pq  
    OP
       2019-10-15 22:08:50 +08:00
    @inhzus 难怪,我传的是 list,会报错 TypeError: argument must be sequence of length 2
    转成 tuple 就可以了,感谢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2929 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 13:50 · PVG 21:50 · LAX 06:50 · JFK 09:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.