<p>您可以将结果保存到列表中,并在找到所有匹配项后打印结果。你知道吗</p>
<pre><code>import re
keycountryfile = '''UK USA Germany'''
countryfile = '''Brexit - UK
USA UK Relations
France win world cup'''
links = '''www.abc.com
www.eee.com'''
links_data = links.split()
keys = '''www.abc.com
www.ddd.com
www.eee.com'''
keys_data = keys.split()
keys_split = keys_data
matching_links = []
not_links = []
for url in keys_split:
if url in links_data:
matching_links.append(url)
else:
not_links.append(url)
keys = set(keycountryfile.split())
matching_country = []
not_country = []
for line in countryfile.split():
words = set(word.lower() for word in re.findall(r'\w+', line))
if keys & words:
matching_country.append(line)
else:
not_country.append(line)
print('LINKS')
if matching_links:
print('\n'.join(matching_links))
print("matching")
print("COUNTRY")
print()
if matching_country:
print('\n'.join(matching_country))
print("matching")
print('LINKS')
if not_links:
print('\n'.join(not_links))
print("Not matching")
print("COUNTRY")
if not_country:
print('\n'.join(not_country))
print("Not matching")
</code></pre>
<p>你可以试试这个代码<a href="https://repl.it/@GilbertoHideo/HoneydewAzureTranslation" rel="nofollow noreferrer">here</a></p>