可变重复取决于循环

时间:2015-10-25 02:05:39

标签: python loops

我正在搜索2个网站的字符串。

每个网站都有相同的字符串,现在的主要问题是:

s = "name:'(.*)',"

WebType = re.findall(s, html1)   

for name in WebType:    
    if 'RoundCube' or 'SquirrelMail' in name:    
        print host + "--> " + name

基本上,我认为结果会依赖于循环结果而重复。

结果是:

https://website1.com--> Roundcube
https://website1.com--> SquirrelMail

https://website2.com--> Roundcube
https://website2.com--> SquirrelMail

如何将结果设为:

https://website1.com--> RoundCube, SquirrelMail
https://website2.com--> Roundcube, SquirrelMail

2 个答案:

答案 0 :(得分:0)

上面的dursk解决方案应该有效。但是,更长的解决方案是:

s = "name:'(.*)',"
WebType = re.findall(s, html1)
results = []
for name in WebType:
    if 'RoundCube' or 'SquirrelMail' in name:
        results.append(name)
print host + "--> " + ', '.join(results)

答案 1 :(得分:0)

试试这段代码:

import random 
hosts = ['https://website1.com', 'https://website2.com']
WebList = ['RoundCube', 'SquirrelMail'] + ['webtype_{}'.format(x) for x in range(2)]
WebType = WebList[random.randint(0, len(WebList)):]
name_list = ['RoundCube', 'SquirrelMail']

for host in hosts:
    name = filter(set(WebType).__contains__, name_list)
    if len(name):
        print host + "--> " + str(name).lstrip('[').rstrip(']').replace('\'', '')

我为了测试目的创建了WebList。 在您的代码中,除非您想测试此处的内容,否则无需导入random

输出:

https://website1.com--> RoundCube, SquirrelMail
https://website2.com--> RoundCube, SquirrelMail

它还输出:

https://website1.com--> SquirrelMail
https://website2.com--> SquirrelMail

您可能需要根据需要进行调整。