[英]How to extract special set of string/words from a list based on a given pattern?
I have below list of name_space characters:我有以下名称空间字符列表:
existing_list = ['ns:Insurance/ns:Policy/ns24:Product/ns24:dpname','ns:Insurance/ns:Policy/ns25:Event/ns25:evtname','ns:Insurance/ns:Policy/ns26:Account/ns26:actnum']
updated_list = ['ns:Insurance/ns:Policy/ns14:Product/ns14:dpname','ns:Insurance/ns:Policy/ns15:Event/ns15:evtname','ns:Insurance/ns:Policy/ns16:Account/ns16:actnum']
Now I have to verify the pattern of item from the existing_list
if those have been changed in the 'updated_list'.现在我必须验证
existing_list
列表中的项目模式是否在“更新列表”中已更改。
For example: The namspace:例如:命名空间:
ns:Insurance/ns:Policy/ns24:Product/ns24:dpname
from existing_list
has been updated with来自
existing_list
列表已更新为
ns:Insurance/ns:Policy/ns14:Product/ns14:dpname
in the 'updated_list' - ie number changed from 24
to 14
在“updated_list”中 - 即数字从
24
更改为14
I am looking for a way to extract the pattern of string without including the 'ns' value, in this case 'Insurance/Policy/Product/dpname' from the 'updated_list'.我正在寻找一种方法来提取字符串模式而不包括“ns”值,在本例中为“updated_list”中的“Insurance/Policy/Product/dpname”。
Here the pattern 'Insurance/Policy/Product/dpname' is available in the 'updated_list'这里的模式 'Insurance/Policy/Product/dpname' 在 'updated_list' 中可用
if I search for the pattern without 'ns' from existing_list
- ie, 'Insurance/Policy/Product/dpname'如果我从
existing_list
列表中搜索没有“ns”的模式 - 即“保险/政策/产品/dpname”
# ie:
pattern_string = 'Insurance/Policy/Product/dpname'
for element in updated_list:
result = re.match(pattern_string, element)
it should return:它应该返回:
''ns:Insurance/ns:Policy/ns14:Product/ns14:dpname'' # from the 'updated_list'
because we are checking to get the updated ns value from the updated_list
.因为我们正在检查从
updated_list
获取更新的 ns 值。
What is the right approach to get this?得到这个的正确方法是什么?
@Cloud_Hari kindly try this solution, @Cloud_Hari 请尝试此解决方案,
import re
pattern = r"([\d]+)"
numbers = []
existing_list = ['ns:Insurance/ns:Policy/ns24:Product/ns24:dpname','ns:Insurance/ns:Policy/ns25:Event/ns25:evtname','ns:Insurance/ns:Policy/ns26:Account/ns26:actnum']
updated_list=['ns:Insurance/ns:Policy/ns14:Product/ns14:dpname','ns:Insurance/ns:Policy/ns15:Event/ns15:evtname','ns:Insurance/ns:Policy/ns16:Account/ns16:actnum']
for previous, current in zip(existing_list, updated_list):
if re.findall(pattern, previous)[0] != re.findall(pattern, current)[0]:
print("previous list value => ", previous)
print("Has been updated to => ", current)
#Feel free to add your logic here
This uses REGEX to match the occurrences of the number in the previous list and compare it with the second list, and if they are different they will print them out, and you can add your logic inside the if block.这使用 REGEX 匹配前一个列表中出现的数字并将其与第二个列表进行比较,如果它们不同,它们将打印出来,您可以在 if 块中添加逻辑。
The REGEX pattern => ([\d+]) forms a group of matching decimals. REGEX 模式 => ([\d+]) forms 一组匹配的小数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.