这个Python代码可以进一步缩小吗?

时间:2015-06-04 09:26:30

标签: python coding-style

以下是获取域内所有子域的Python代码。它将文件作为包含网站页面源的输入。第二个参数是域名。例如:"https://www.sometime.com"

import re
def getSubDomains(fil,domain):
   with open(fil) as f:
    subDomainLst = []
    for line in f:
      m = re.findall(r'\bhref="\https://[\w+\.*]+%s/'%domain,line)
      if(m):
         for ele in m: subDomainLst.append(ele.split('/')[2])
      else:
            continue
   subDomainLst = list(set(subDomainLst))
   for ele in subDomainLst: print ele 
def main():
    fil1,domain1 = raw_input("Enter the file name\n"),raw_input("Enter the domain\n")
    getSubDomains(fil1,domain1)
main() if __name__ == '__main__' else Pass

我试图缩小内心的“if else”陈述'到

for ele in m: subDomainLst.append(ele.split('/')[2]) if(m) else continue

但这会产生错误。

上面的代码可以进一步缩小(现在是16行),这样它占用最少的行数并变得更加pythonic吗?

3 个答案:

答案 0 :(得分:1)

您不需要添加继续。您可以尝试这样做,即使我不推荐,因为它使代码不可读。

$("#klik").click(function() {
console.log("fire away");
page = page + 1;

//$("#archief").load("trytocombinenewageandgettagsendates.php");
 console.log(page);              
                $.ajax({
                    type: 'POST',
                    url: "trytocombinenewageandgettagsendates.php",
                   data:  { 'page': page },

                    success: function(response){

                    $("#archief").append(response);  

                    },

                    error: function(err) {
                            alert(err.responseText);
                    } 
                });
                return false;

顺便说一句,你应该将你的代码缩进4个空格并尽量避免使用单行不可理解的语句:pythonic意味着也可读

完整代码:

subDomainLst = [ele.split('/')[2] for line in f for ele in re.findall(r'\bhref="\https://[\w+\.*]+%s/' % domain, line)]

答案 1 :(得分:0)

您可能希望将try..except语句更改为try: for ele in m: subDomainLst.append(ele.split('/')[2]) except TypeError: print "OMG m is not iterable!"

l1 = [1,2,3]
l2 = [(1,2),(1,3),(2,3)]

或类似的东西

答案 2 :(得分:0)

你有两个不同的目标:缩小线条,变得更加pythonic。 这是一行,但它不是pythonic:

import re;fil,domain = raw_input("Enter the file name\n"),raw_input("Enter the domain\n");print '\n'.join(set(ele.split('/')[2] for line in open(fil) for ele in (re.findall(r'\bhref="\https://[\w+\.*]+%s/'%domain,line) or ())))