0

I have a pretty simple Python script/task where I'm trying to search for a string of text while iterating over a list.

domains = ['google.com', 'facebook.com', 'cnn.com']
domain = []

for x in domains:
  if any("google"):
    domain.append("Google")

The output of the code above is: ['Google', 'Google', 'Google'] I'm expecting the output to just have one entry of "Google" since there is only one domain that would match.

I've tried

re.search('google',x)

and:

for x in domains:
  if any("google" in x for x in domains):
    domain.append("Google")

The output for each is the same. As long as there is an entry of "Google" in the entire list then each append entry will be "Google". I apologize as I'm sure this is a simple question but I can't quite seem to get it correctly. This is the last piece of my project and I'm stumped. Any help is appreciated.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 4
    You just have to do: `if "google" in domains: domain.append("google")`. There's no point in using `any()` at all. – Grajdeanu Alex Dec 28 '20 at 22:20
  • For verifying if multiple domain targets are in domains: `[domain.split('.')[0].capitalize() for domain in domains for target in targets if target in domain]` where `targets` is a list like `['google', 'facebook']`. – Cainã Max Couto-Silva Dec 28 '20 at 22:35
  • @GrajdeanuAlex The issue with that is that `"google"` isn't the only text of the matching list item. In OP's example, `"google.com"` should be a match as it contains `"google"`. – Ben Soyka Dec 28 '20 at 22:59
  • Does this answer your question? [Regular Expressions: Search in list](https://stackoverflow.com/questions/3640359/regular-expressions-search-in-list) (specifically, this answer: https://stackoverflow.com/a/39593126/2745495). – Gino Mempin Dec 28 '20 at 23:49

3 Answers3

2

Your problem is that you are comparing all entries in the domains list to google instead of just the current one:

domains = ['google.com', 'facebook.com', 'cnn.com']
domain = []

for d in domains:
    if 'google' in d:
        domain.append('Google')

Output

['Google']
Nick
  • 138,499
  • 22
  • 57
  • 95
0
domains = ['google.com', 'facebook.com', 'cnn.com']
domain = []

for x in domains:
  if "google" in x:
    domain.append("Google")
abumalick
  • 2,136
  • 23
  • 27
0

Here are three valid approaches:

domains = ['google.com', 'facebook.com', 'cnn.com', 'www.google.com']

# Approach 1: regex
matching_domains = []
for x in domains:
  if re.search('google', x):
    matching_domains.append(x)
print(matching_domains)

# Approach 2: `in`
matching_domains = []
for domain in domains:
  if "google" in domain:
    matching_domains.append(domain)
print(matching_domains)

# Approach 3: `filter`
# - see https://docs.python.org/3/library/functions.html#filter
# - see https://www.geeksforgeeks.org/filter-in-python/
matching_domains = []
matching_domains = filter(lambda x: "google" in x, domains)
print(matching_domains)

Your approach using re (regex) was very close. Best of luck with your project! Hope you stick with software development.

Arthur S
  • 337
  • 3
  • 8