簡體   English   中英

根據另一個字符串列表(例如黑名單字符串)從字符串列表中每個元素的末尾刪除字符

[英]Remove characters from the end of each element in a list of strings based on another list of strings (e.g. blacklist strings)

我有一個字典,其中包含鍵“樣本”的許多唯一字符串值。 我正在將此鍵“樣本”轉換為用於繪圖的列表,但是我想生成另一個具有相同數量元素的列表,這些元素在每個元素的末尾去除某些字符串以生成一個“干凈”列表,然后可以對某些元素進行分組樣品一起作圖。 例如,我的黑名單如下所示:

blacklist = ['_001', '_002', '_003', '_004', '_005', '_006', '_007', '_008', '_009', \
                       '_01', '_02', '_03', '_04', '_05', '_06', '_07', '_08', '_09', \
                       '_1', '_2', '_3', '_4', '_5', '_6', '_7', '_8', '_9']

我想從我的字典生成的這個示例列表中的每個項目中刪除它:

sample = [(d['sample']) for d in my_stats]
sample
['sample_A', 'sample_A_001', 'sample_A_002', 'my_long_sample_B_1', 'other_sample_C_08', 'sample_A_03', 'sample1_D_07']

具有新列表的所需結果:

sample
['sample_A', 'sample_A', 'sample_A', 'my_long_sample_B', 'other_sample_C', 'sample_A', 'sample1_D']

對於上下文,我知道會有一些元素是相同的——我想使用這個列表來編譯一個 dataframe 以及具有相同數量的值的列表,從這個字典中生成其他鍵,這些鍵將用作繪圖中的 id(即,我可以使用它對所有這些值進行相同的分組/着色)。 請注意,可能有不同數量的下划線,並且我的字符串列表中可能有一些元素不包含黑名單中的任何值(這就是為什么我不能在最后一個下划線上使用某些拆分變體的原因)。

這與此問題類似: 如何刪除列表中的多個字符?

但我不希望它如此籠統/貪婪,理想情況下只希望從末尾刪除它,因為用戶可能在內部有一個包含這些字符串部分的輸入文件(例如 sample1_D 中的 1)。 如果有其他解決方案,我不一定需要使用黑名單,這似乎是最簡單的方法。

使用regex

import re

pattern = '|'.join(blacklist)
[re.sub(pattern+'$', '', x) for x in sample]

Output

['sample_A',
 'sample_A',
 'sample_A',
 'my_long_sample_B',
 'other_sample_C',
 'sample_A',
 'sample1_D']

給你 go,看看這是否符合你的要求。

基本上,您只是拆分'_'字符並測試列表中的最后一個拆分是否在您的黑名單中。 如果True ,則將其刪除,如果False將字符串重新組合在一起; 並根據結果建立一個新列表。

blacklist = ['_001', '_002', '_003', '_004', '_005', '_006', '_007', '_008',
             '_01', '_02', '_03', '_04', '_05', '_06', '_07', '_08', '_09',
             '_1', '_2', '_3', '_4', '_5', '_6', '_7', '_8', '_9']
sample = ['sample_A', 'sample_A_001', 'sample_A_002', 'my_long_sample_B_1',
          'other_sample_C_08', 'sample_A_03', 'sample1_D_07']
results = []

for i in sample:
    splt = i.split('_')
    value = '_'.join(splt[:-1]) if '_{}'.format(splt[-1:][0]) in blacklist else '_'.join(splt)
    results.append(value)

print(results)

Output:

['sample_A', 'sample_A', 'sample_A', 'my_long_sample_B', 'other_sample_C', 'sample_A', 'sample1_D']

您可以使用正則表達式中的sub

import re
from functools import partial

blacklist = ['_001', '_002', '_003', '_004', '_005', '_006', '_007', '_008', '_009',
             '_01', '_02', '_03', '_04', '_05', '_06', '_07', '_08', '_09',
             '_1', '_2', '_3', '_4', '_5', '_6', '_7', '_8', '_9']


def sub(match, bl=None):
    if match.group() in bl:
        return ""
    return match.group()


repl = partial(sub, bl=set(blacklist))

sample = ['sample_A', 'sample_A_001', 'sample_A_002', 'my_long_sample_B_1', 'other_sample_C_08', 'sample_A_03',
          'sample1_D_07']

print([re.sub("_[^_]+?$", repl, si) for si in sample])

Output

['sample_A', 'sample_A', 'sample_A', 'my_long_sample_B', 'other_sample_C', 'sample_A', 'sample1_D']

了解為什么這是通往 go 的方式,如果您想要速度,請點擊此處

您可以遍歷示例列表,如果元素的最后一個字符是數字,那么您可以遍歷黑名單項目,檢查字符串是否以該字符結尾。 如果是這樣,那么您可以從字符串中刪除黑名單項並將結果重新分配給示例列表。

blacklist = [
    '_001', '_002', '_003', '_004', '_005', '_006', '_007', '_008', '_009',
    '_01', '_02', '_03', '_04', '_05', '_06', '_07', '_08', '_09',
    '_1', '_2', '_3', '_4', '_5', '_6', '_7', '_8', '_9'
]

sample = ['sample_A', 'sample_A_001', 'sample_A_002', 'my_long_sample_B_1', 'other_sample_C_08', 'sample_A_03', 'sample1_D_07']

for index, item in enumerate(sample):
    #check if the last char is a digit, if its not then it cant be in our black list so no point checking
    if item[-1].isdigit():
        for black in blacklist:
            if item.endswith(black):
                sample[index] = item.rstrip(black)

print(sample)

OUTPUT

['sample_A', 'sample_A', 'sample_A', 'my_long_sample_B', 'other_sample_C', 'sample_A', 'sample1_D']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM