简体   繁体   English

如何在列表python的项目中查找字符串

[英]How to find string in item of list python

I need your help because want to find a str in a list for to get complete item. 我需要您的帮助,因为想要在列表中找到一个str以获得完整的项目。

my_list: looks like: my_list:看起来像:

['Mont Saint Martin, 42 rue de Bordeaux', 'Mont de Marsan, 100 avenue Pierre de Coubertin', 'Lyon, 56 rue du Docteur Alb\xc3\xa9ric Pont', 'Lille, 90 rue d\xe2\x80\x99Arras', 'Lyon, 2 all\xc3\xa9e des fleurs']

str: looks like: str:看起来像:

"saint" or "de" “圣人”或“德”

Expected list after processing i want to check before ',': 处理后的预期清单我想在','之前检查:

['Mont Saint Martin']

or for str = "de" 或对于str =“ de”

['Mont de Marsan']

I have try with: 我尝试过:

new = filter(lambda x: x.my_list == str, x)[0]

Thanks for you help. 感谢您的帮助。

cities = [w.split(',')[0] for w in my_list]
term = "de"
results = [city for city in cities if term in city.lower()]

Use re.match function. 使用re.match功能。

>>> l = ['Mont Saint Martin, 42 rue de Bordeaux', 'Mont de Marsan, 100 avenue Pierre de Coubertin', 'Lyon, 56 rue du Docteur Alb\xc3\xa9ric Pont', 'Lille, 90 rue d\xe2\x80\x99Arras', 'Lyon, 2 all\xc3\xa9e des fleurs']
>>> st = ["saint","de"]
>>> for i in st:
        for j in l:
            for x in j.split(','):
                if re.match(r'(?i)\S+ ' + i + r' \S+$', x):
                    print(x)


Mont Saint Martin
Mont de Marsan
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM