社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

有没有类似于matlab的python语法来选择行和列?[副本]

caesar • 5 年前 • 1804 次点击  

如果我有一个(浮动)列表

list = [
       [111, 111, 4523.123, 111, 111],
       [111, 111, 4526.15354, 111, 111],
       [111, 111, 4580.112, 111, 111],
       ]

不使用循环如何获取第三列?如果是matlab中的矩阵 list(:,3) .

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52318
 
1804 次点击  
文章 [ 5 ]  |  最新文章 5 年前
Adirio
Reply   •   1 楼
Adirio    5 年前

没有,但是你可以创建一个类来处理它。基本上你需要覆盖 __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
Alexander Pushkarev
Reply   •   2 楼
Alexander Pushkarev    5 年前

Numpy是一个很好的解决方案:

import numpy as np


list = [
       [111, 111, 4523.123, 111, 111],
       [111, 111, 4526.15354, 111, 111],
       [111, 111, 4580.112, 111, 111],
       ]

np_list = np.array(list)[:,2]

对于更复杂的数据操作,我建议使用 pandas.DataFrame ( https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html )

如果你必须使用列表,你可以尝试列表理解。从技术上讲,这是一个循环,但使用的语法要短得多,而且是处理集合的一种非常python的方式。它还使您不必使用外部库:

>>> list = [
...        [111, 111, 4523.123, 111, 111],
...        [111, 111, 4526.15354, 111, 111],
...        [111, 111, 4580.112, 111, 111],
...        ]
>>> l = [ x[2] for x in list ]
>>> l
[4523.123, 4526.15354, 4580.112]
DarrylG
Reply   •   3 楼
DarrylG    5 年前

使用zip将列转换为行,然后选择第三个子列表(行)

lst = [
       [111, 111, 4523.123, 111, 111],
       [111, 111, 4526.15354, 111, 111],
       [111, 111, 4580.112, 111, 111],
       ]

第三栏:

list(zip(*lst))[2] 
Dante
Reply   •   4 楼
Dante    5 年前

使用库 numpy :

import numpy as np


list = [
       [111, 111, 4523.123, 111, 111],
       [111, 111, 4526.15354, 111, 111],
       [111, 111, 4580.112, 111, 111],
       ]

np_list = np.array(list)
third_col = np_list[:,2] # counting starts from 0

velociraptor11
Reply   •   5 楼
velociraptor11    5 年前

你当然可以用Numpy来帮助你实现这个目标。

import numpy as np
list = np.array([
       [111, 111, 4523.123, 111, 111],
       [111, 111, 4526.15354, 111, 111],
       [111, 111, 4580.112, 111, 111],
       ])
list[:,2]

我不认为不使用任何循环就可以使用本机python列表实现这一点。