社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

LiuXiMin

LiuXiMin 最近创建的主题
LiuXiMin 最近回复了
6 年前
回复了 LiuXiMin 创建的主题 » 如何使用Python下载youtube视频?

尝试 you-get

5 年前
回复了 LiuXiMin 创建的主题 » python中的board[x,y]`和board[x][y]`有区别吗?

在python中, [] __getitem__ ,很容易重写。

而且, 1, 2 在python中会给我们一个元组。是的,我们不需要 () 创建非空元组。

所以,努比可以很容易地做到这一点,甚至我也可以。

In [1]: 1, 1
Out[1]: (1, 1)

In [2]: type(_)
Out[2]: tuple

In [3]: a = {(1, 1): 3}

In [4]: a[1, 1]
Out[4]: 3

In [5]: a[(1, 1)]
Out[5]: 3

In [6]: class NumpyArray(list):
   ...:     def __getitem__(self, index):
   ...:         if isinstance(index, tuple) and len(index) == 2:
   ...:             return self[index[0]][index[1]]
   ...:         return super().__getitem__(index)
   ...:

In [7]: b = NumpyArray([[0, 1], [2, 3]])

In [8]: b[1, 1]
Out[8]: 3

您可以使用下面的代码在自己的ipython上进行尝试。

class NumpyArray(list):
    def __getitem__(self, index):
        if isinstance(index, tuple) and len(index) == 2:
            return self[index[0]][index[1]]
        return super().__getitem__(index)

b = NumpyArray([[0, 1], [2, 3]])
b[1, 1]