4 回答

TA貢獻1804條經驗 獲得超8個贊
使用regex.
import re
pattern = '|'.join(blacklist)
[re.sub(pattern+'$', '', x) for x in sample]
輸出:
['sample_A',
'sample_A',
'sample_A',
'my_long_sample_B',
'other_sample_C',
'sample_A',
'sample1_D']

TA貢獻1865條經驗 獲得超7個贊
來吧,看看這是否符合您的要求。
基本上,您只是拆分'_'角色并測試列表中的最后一個拆分是否在您的黑名單中。如果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)
輸出:
['sample_A', 'sample_A', 'sample_A', 'my_long_sample_B', 'other_sample_C', 'sample_A', 'sample1_D']

TA貢獻1843條經驗 獲得超7個贊
您可以遍歷示例列表,如果元素的最后一個字符是數字,那么您可以遍歷黑名單項目,檢查字符串是否以該字符結尾。如果是這樣,那么您可以從字符串中刪除黑名單項并將結果重新分配給示例列表。
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)
輸出
['sample_A', 'sample_A', 'sample_A', 'my_long_sample_B', 'other_sample_C', 'sample_A', 'sample1_D']

TA貢獻1856條經驗 獲得超11個贊
您可以使用正則表達式中的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])
輸出
['sample_A', 'sample_A', 'sample_A', 'my_long_sample_B', 'other_sample_C', 'sample_A', 'sample1_D']
看看為什么這是要走的路,如果你想要速度,在這里。
添加回答
舉報