Python 在遍历列表时搜索字符串

Python 在遍历列表时搜索字符串,python,Python,我有一个非常简单的Python脚本/任务,其中我试图在遍历列表的同时搜索文本字符串 domains = ['google.com', 'facebook.com', 'cnn.com'] domain = [] for x in domains: if any("google"): domain.append("Google") 上面代码的输出是:['Google','Google','Google'] 我希望输出只有一个“Google”条

我有一个非常简单的Python脚本/任务,其中我试图在遍历列表的同时搜索文本字符串

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

for x in domains:
  if any("google"):
    domain.append("Google")
上面代码的输出是:['Google','Google','Google'] 我希望输出只有一个“Google”条目,因为只有一个域可以匹配

我试过了

re.search('google',x)
以及:


每个的输出都是相同的。只要整个列表中有一个“Google”条目,那么每个附加条目都将是“Google”。我很抱歉,因为我相信这是一个简单的问题,但我似乎不能完全正确地理解它。这是我项目的最后一部分,我被难倒了。非常感谢您的帮助。

您的问题是您正在将
列表中的所有条目与
谷歌
进行比较,而不仅仅是当前条目:

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

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

['Google']

您的问题是您正在将
列表中的所有条目与
谷歌
进行比较,而不仅仅是当前条目:

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

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

['Google']
domains=['google.com','facebook.com','cnn.com']
域=[]
对于域中的x:
如果x中的“google”:
domain.append(“谷歌”)
domains=['google.com','facebook.com','cnn.com']
域=[]
对于域中的x:
如果x中的“google”:
domain.append(“谷歌”)

以下是三种有效的方法:

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)

您使用
re
(regex)的方法非常接近。祝你的项目好运!希望您坚持软件开发。

以下是三种有效的方法:

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)

您使用
re
(regex)的方法非常接近。祝你的项目好运!希望你坚持软件开发。

你只要做:
如果在域中使用“google”:domain.append(“google”)
。使用
any()
没有任何意义。要验证域中是否有多个域目标:
[domain.split('..')[0]。如果目标在域中,则在域中为目标在域中为目标在域中为目标大写()
其中
targets
是一个类似
['google',facebook']
@GrajdeanuAlex的列表,问题在于
“google”
不是匹配列表项的唯一文本。在OP的示例中,
“google.com”
应该是匹配项,因为它包含
“google”
。这是否回答了您的问题?(具体地说,这个答案:)。您只需做:
如果域中的“google”:domain.append(“google”)
。使用
任何()
完全可以。要验证域中是否有多个域目标:
[domain.split('..)[0]。如果域中有目标,则域中目标的域中目标的域中目标的大写()
其中
目标
是一个类似
['google',facebook']
@GrajdeanuAlex的列表,问题是
谷歌“
不是匹配列表项的唯一文本。在OP的示例中,
“google.com”
应该是匹配的,因为它包含
“google”
。这是否回答了您的问题?(具体来说,答案是:)。