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

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

我的解决方案:

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

我不知道如何返回原始索引。请帮忙! 变量: 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)

【问题讨论】:

标签: python pandas list indexing filtering


【解决方案1】:

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

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

【讨论】:

  • 是的,但是我怎么知道哪些索引被过滤了?过滤完成后,将重新分配索引。它有数千个文件,所以我希望有一种简单的方法来简单地返回一个列表。
猜你喜欢
  • 2015-10-03
  • 2012-08-11
  • 2017-08-05
  • 1970-01-01
  • 2021-07-21
  • 2017-06-14
  • 2018-10-15
  • 2020-05-09
  • 1970-01-01
相关资源
最近更新 更多