-1

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
levent
  • 33
  • 4
  • 1
    There are a number of ways to do this, and it depends on how you want to handle other specific cases. For instance, would you want `'David Benj'` or `'David Benj Jacob'` to also return the first element? What if it's a hyphenated last name? In any case, `str.split` might be useful for you. – busybear Oct 27 '22 at 19:56
  • 1
    Is `"David Benj Jacob"` returned because David and Jacob are in the string or because David or Jacob are in the string? What is returned when you have `["David Benj Jacob", "Alex", "David", "Jacob"]`? – It_is_Chris Oct 27 '22 at 19:57
  • @busybear I want to return 'David Benj Jacob' if I search with 'David Jacob'. just leave hyphenated or any other cases for now – levent Oct 27 '22 at 19:59

4 Answers4

2

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)
jprebys
  • 2,469
  • 1
  • 11
  • 16
1
names = ['David benj Jacob', 'John Legend']
wanted_name = "David Jacob"
first_name, last_name = wanted_name.split(' ')
for name in names:
  if first_name in names or last_name in names:
    print(name)

or using regex and filter functions in python. like the what the answer suggested provided here: Regular Expressions: Search in list which would be something like:

names = ['David benj Jacob', 'John Legend']
wanted_name = "David Jacob"
first_name, last_name = wanted_name.split(' ')
pat = re.compile(fr"{first_name}.*?{last_name}")
wanted_names = list(filter(pat.match,names))
  • This works, but only for this very specific example. It would be better to provide a solution that can handle more than one use case. – JRiggles Oct 27 '22 at 20:05
  • 1
    @JRiggles thank you for mentioning that. I've edited the answer to one that uses ```split``` to be not as hard coded. – bentrification Oct 27 '22 at 20:17
0

I've come to a similar conclusion as @jprebys, but I've written a basic function that returns the index of the match in the names list given a search string.

names = ["David Benj Jacob", "Alex"]

def find_name(search: str) -> int:
    for name in names:
        if all(n in name for n in search.split()):
            return names.index(name)      
JRiggles
  • 4,847
  • 1
  • 12
  • 27
-1

Try splitting the querying string to substrings and check if that substring is present in items from an names array.

names = ["David Benj Jacob", "Alex"]

def search(names: list[str], query: str) -> str:
    # Split words 
    split = query.split(" ")

    # Iterate over all names
    for item in names:
        # Iterate over all words from 'query'
        for word in split:
            # Check if word is present
            if word in item:
                return item

queried = search(names, "David Jacob")
Zapomnij
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 31 '22 at 10:04