Issue
I have a list that has names and I want to get the list element which has some part of my string
for example:
names = ["David Benj Jacob", "Alex"]
so if I search with "David Jacob" then how can I get the first element of the list "David Benj Jacob"?
I tried if "David Jacob" in names
Solution
You need to check if all parts of the substring are in any of the names
names = ["David Benj Jacob", "Alex"]
substr = "David Jacob"
valid_names = []
for name in names:
if all(part in name for part in substr.split()):
valid_names.append(name)
Answered By - jprebys
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.