Python Regex循环从列表中创建新列表

时间:2015-07-29 20:26:47

标签: regex list python-2.7

以下是我的代码片段:


    blahblahblah...
    blah[]
    for link in goodies.soup.find_all('a'):
       blah.append(link.get('href'))
       blah=list(set(blah))

给出了我的网址列表。所以现在我使用正则表达式在列表中搜索我想要的相关网址:

     for r in blah:
     capture=re.findall(r'https://.*', r)
     print (capture)

将结果打印为:


    []
    ['https://blah.org/plugins/blahblahblahblah/']
    []
    []
    ['https://blah.org/plugins/blahblahblah/']

我要做的是从这些结果中创建一个列表。当我尝试这样做时,我总是遇到一个砖墙:

    for r in blah:
        capture=re.findall(r'https://.*', r)
        purls=[]
        purls.append(capture)
    purls
    [[]]
    for r in blah:
        capture=re.findall(r'https://.*', r)
        purls=[]
           for a in capture:
               purls.append(a)
    purls
    []

这是我能提出的最接近的事情:

    for r in blah:
         capture=re.findall(r'https://.*', r)
         rolos=capture[:]
         print rolos
    []
    ['https://blah.org/plugins/blahblahblahblah/']
    []
    []
    ['https://blah.org/plugins/blahblahblah/']
    whos
    rolos           list      n=0

但正如你所看到的,它确实创建了列表????我做错了什么,如何实现呢?

1 个答案:

答案 0 :(得分:0)

所以结果是+ =,这是增量运算符

for r in blah:
  capture=re.findall(r'https://.*', r)
  rolos+=capture[:]

感谢dslackw(linuxquestons.org)提供这种疯狂的解决方案。我还是不敢相信。