从字符串列表中删除一个项目
removing an item from a list of strings
我有一个存储在可变 pdf 路径中的字符串列表,还有另一个字符串列表,如果我的 pdf 路径列表中包含任何项目,则应将其删除。
它适用于某些字符串但不适用于其他字符串,即使该字符串包含相同的词也是如此。
def remove_derivaties(blacklist, pdf_list):
for x in blacklist:
for y in pdf_list:
if x in y:
pdf_list.remove(y)
return pdf_list
def main():
string = ["P:\Example Files\Latest\PRM-FRM-011 Calc Review Form.pdf",
"P:\Example Files\Latest\PRM-FRM-003 Project Assignment Form.pdf",
"control_string"]
exclude = ["P:\Example Files",
"control_string1"]
not_excluded = remove_derivaties(exclude, string)
print(not_excluded)
输出为['P:\Example Files\Latest\PRM-FRM-003 Project Assignment Form.pdf', 'control_string']
然而排除词包含在两个字符串中
这是正确的行为吗?
from typing import List
string = ["P:\Example Files\Latest\PRM-FRM-011 Calc Review Form.pdf",
"P:\Example Files\Latest\PRM-FRM-003 Project Assignment Form.pdf",
"control_string"]
exclude = ["P:\Example Files",
"control_string1"]
def substring_in_string(substring: str, string: str) -> bool:
return substring in string
def substrings_in_string(substrings: List[str], string: str) -> List[bool]:
return [substring_in_string(substring, string) for substring in substrings]
not_excluded = [s for s in string if not any(substrings_in_string(exclude, s))]
output:
['control_string']
我有一个存储在可变 pdf 路径中的字符串列表,还有另一个字符串列表,如果我的 pdf 路径列表中包含任何项目,则应将其删除。
它适用于某些字符串但不适用于其他字符串,即使该字符串包含相同的词也是如此。
def remove_derivaties(blacklist, pdf_list):
for x in blacklist:
for y in pdf_list:
if x in y:
pdf_list.remove(y)
return pdf_list
def main():
string = ["P:\Example Files\Latest\PRM-FRM-011 Calc Review Form.pdf",
"P:\Example Files\Latest\PRM-FRM-003 Project Assignment Form.pdf",
"control_string"]
exclude = ["P:\Example Files",
"control_string1"]
not_excluded = remove_derivaties(exclude, string)
print(not_excluded)
输出为['P:\Example Files\Latest\PRM-FRM-003 Project Assignment Form.pdf', 'control_string']
然而排除词包含在两个字符串中
这是正确的行为吗?
from typing import List
string = ["P:\Example Files\Latest\PRM-FRM-011 Calc Review Form.pdf",
"P:\Example Files\Latest\PRM-FRM-003 Project Assignment Form.pdf",
"control_string"]
exclude = ["P:\Example Files",
"control_string1"]
def substring_in_string(substring: str, string: str) -> bool:
return substring in string
def substrings_in_string(substrings: List[str], string: str) -> List[bool]:
return [substring_in_string(substring, string) for substring in substrings]
not_excluded = [s for s in string if not any(substrings_in_string(exclude, s))]
output:
['control_string']