python 使用正则表达式在列表元素之间选择

xesrikrc  于 2022-10-30  发布在  Python
关注(0)|答案(3)|浏览(110)

我有两个清单:

images = ['ND_row_id_4026.png',
          'ND_row_id_7693.png',
          'ND_row_id_5285.png',
          'ND_row_id_1045.png',
          'ND_row_id_2135.png',
          'ND_row_id_8155.png',
          'ND_row_id_3135.png']

masks = ['MA_row_id_4026.png',
         'MA_row_id_7693.png',
         'MA_row_id_5285.png',
         'MA_row_id_1045.png',
         'MA_row_id_2135.png']

我想保存masks文件。所以,我有2个文件夹,里面有图像和遮罩,我想保存masks列表中的所有遮罩和相应的图像。图像列表比遮罩大。
所以,我想从图片列表中保留:

images = ['ND_row_id_4026.png',
          'ND_row_id_7693.png',
          'ND_row_id_5285.png',
          'ND_row_id_1045.png',
          'ND_row_id_2135.png']
mrphzbgm

mrphzbgm1#

看起来(* 根据您的示例数据 *),简单的列表理解应该足够了,只需替换前缀进行比较:

res = [im for im in images if im.replace('ND', 'MA') in masks]

输出(针对示例数据):

[
 'ND_row_id_4026.png',
 'ND_row_id_7693.png',
 'ND_row_id_5285.png',
 'ND_row_id_1045.png',
 'ND_row_id_2135.png'
]

如果你的列表很大,那么预处理masks数组会更有效率(因为它是两个数组中较小的一个):

m2 = [ma.replace('MA', 'ND') for ma in masks]
res = [im for im in images if im in m2]
mctunoxg

mctunoxg2#

试试这个

images = [image for image in images if image in [x.replace('MA','ND') for x in masks]]

说明:
[...]:生成列表
[image for image in images]:沿着图像列表迭代
... if image in [..]:过滤结果,只返回其他列表中的那些。
[x.replace('MA','ND') for x in masks]:生成列表,其中掩码中的MA被替换为ND

agxfikkp

agxfikkp3#

也许这可以帮助你...用set方法写

代码:

import re

images = ['ND_row_id_4026.png',
          'ND_row_id_7693.png',
          'ND_row_id_5285.png',
          'ND_row_id_1045.png',
          'ND_row_id_2135.png',
          'ND_row_id_8155.png',
          'ND_row_id_3135.png']

masks = ['MA_row_id_4026.png',
         'MA_row_id_7693.png',
         'MA_row_id_5285.png',
         'MA_row_id_1045.png',
         'MA_row_id_2135.png']

# Fetching all digits from image file names

forimages = set( j[0] for j in [ re.findall(r'\d+',val) for val in images ])

# Fetching all digits from mask file names

formasks =  set( j[0] for j in [ re.findall(r'\d+',val) for val in masks ])

# Applying intersection logic on digit to get comma digit between both

my_mask_data = [ 'ND_row_id_{0}.png'.format(i) for i in sorted(forimages.intersection(formasks)) ]

# Printing the output

print(my_mask_data)

输出:

['ND_row_id_1045.png', 'ND_row_id_2135.png', 'ND_row_id_4026.png', 'ND_row_id_5285.png', 'ND_row_id_7693.png']

如果你发现上面有任何问题...请随时更改

相关问题