Python:返回从列表中过滤的对象的 index() 列表

Posted

技术标签:

【中文标题】Python:返回从列表中过滤的对象的 index() 列表【英文标题】:Python: return a list of the index() of objects filtered from a list 【发布时间】:2021-12-16 20:05:11 【问题描述】:

我正在使用以下函数从数据库查询中过滤掉许多文件类型。文件扩展名有大小写变化(例如,Tif、tIF、TIF、tif)。但是,我需要在系统中按原样使用大写进一步查询。

我的解决方案:

    复制数据帧 (Pandas) 小写(已经完成)。 根据两个列表执行过滤器(已经完成)。 返回过滤对象在被过滤之前所在位置的索引(索引?)列表。 使用索引列表从我的原始列表中删除对象。这会保留我的大写字母并删除正确的项目。

我不知道如何返回原始索引。请帮忙! 变量: path_list_lowercase suffixes_unwanted

def index_pos_filtered_paths(path_list_lowercase, suffixes_unwanted):
        for [x for x in path_list_lowercase if all(y not in x for y in suffixes_unwanted)]
        return path_list_lowercase(x).index(suffixes_unwanted(y))


index_of_filtered_paths_list = index_pos_filtered_paths(path_list_lowercase, suffixes_unwanted)

【问题讨论】:

how to ask good pandas questions 【参考方案1】:

可以使用枚举函数获取索引

['value':x, 'index':i for i,x in enumerate(path_list_lowercase)] 

【讨论】:

是的,但是我怎么知道哪些索引被过滤了?过滤完成后,将重新分配索引。它有数千个文件,所以我希望有一种简单的方法来简单地返回一个列表。

以上是关于Python:返回从列表中过滤的对象的 index() 列表的主要内容,如果未能解决你的问题,请参考以下文章

从列表中删除 NoneType 元素的本机 Python 函数?

python列表的内建函数

index在python中的用法

index在python中的用法

python常用列表函数

Python 列表 insert() 方法