Let's assume I have a list like this
List=["Face123","Body234","Face565"]
I would like to obtain as output a list without character/substring described in another list.
NonDesideredPattern["Face","Body"]
Output=[123,234,565].
I am not sure, this is 100% efficient, but you could do something like this:
def eval_list(og_list):
list_parts = []
list_nums = []
for element in og_list:
part = ""
num = ""
for char in element:
if char.isalpha():
part += char
else:
num += char
list_parts.append(part)
list_nums.append(num)
return list_parts, list_nums
(if you are always working with alphabetical syntax and then a number)
Create a function which returns a string without the undesired patterns. Then use this function in a comprehension list:
import re
def remove_pattern(string, patterns):
result = string
for p in patterns:
result = re.sub(p, '', result)
return result
inputs = ["Face123", "Body234", "Face565"]
undesired_patterns = ["Face", "Body"]
outputs = [remove_pattern(e, undesired_patterns) for e in inputs]
Use re.compile and re.sub
import re
lst = ["Face123", "Body234", "Face565"]
lst_no_desired_pattern = ["Face","Body"]
pattern = re.compile("|".join(lst_no_desired_pattern))
lst_output = [re.sub(pattern, "", word) for word in lst]
Result:
['123', '234', '565']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.