社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Adirio

Adirio 最近创建的主题
Adirio 最近回复了
4 年前
回复了 Adirio 创建的主题 » 有没有类似于matlab的python语法来选择行和列?[副本]

没有,但是你可以创建一个类来处理它。基本上你需要覆盖 __getitem__ 方法,检查传递的值是否为元组,在这种情况下,将相应返回:

class MatlabList(list):
    def __init__(self, *args):
        if len(args) > 1:
            return super().__init__(args)
        return super().__init__(*args)

    def __getitem__(self, item):
        if isinstance(item, tuple):
            # Remove the non-needed tuple in case it onle has one element
            if len(item) == 1:
                return self[item[0]]
            if isinstance(item[0], slice):
                return MatlabList(map(lambda x: x[item[1:]], self[item[0]]))
            return self[item[0]][item[1:]]
        return super().__getitem__(item)

lst = MatlabList(
    MatlabList(111, 111, 4523.123, 111, 111),
    MatlabList(111, 111, 4526.15354, 111, 111),
    MatlabList(111, 111, 4580.112, 111, 111),
)

lst[1,2]  # cell at 2nd row, 3rd column
lst[1,:]  # 2nd row
lst[:,2]  # 3rd column