V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Oct 15, 2019 · 2077 views
    This topic created in 2395 days ago, the information mentioned may be changed or developed.
    def test(pos,target):
    	x, y = pos
    	color = img[x, y]
        ....
    
    • 请问上面这两步有办法简写嘛?
    • 假设 pos 很长,都要拆开写入 img[]内,的情况下,类似*参数的写法有嘛?
    inhzus
        1
    inhzus  
       Oct 15, 2019
    如果没有理解错的话,pos 类型为 tuple,img[x, y] 调用的内置函数为 __getitem__,那就直接 img[pos] 就好了。
    如图 ![image.png]( https://i.loli.net/2019/10/15/gTp1e3W8oaDAdvu.png)
    inhzus
        2
    inhzus  
       Oct 15, 2019   ❤️ 1
    @inhzus #1 上一张复制的有些问题,见这张 ![image.png]( https://i.loli.net/2019/10/15/RkbDN6miOwptyzH.png)
    JCZ2MkKb5S8ZX9pq
        3
    JCZ2MkKb5S8ZX9pq  
    OP
       Oct 15, 2019
    @inhzus 难怪,我传的是 list,会报错 TypeError: argument must be sequence of length 2
    转成 tuple 就可以了,感谢。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6324 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 02:39 · PVG 10:39 · LAX 19:39 · JFK 22:39
    ♥ Do have faith in what you're doing.