使用break来嵌套for循环

时间:2015-09-24 07:24:19

标签: python for-loop break

我想在record上应用三个级别的过滤。

    cname中的
  1. recrec是> 2个单词的字符串,因此我想考虑在rec中检查record的所有ngram

    我。如果gramrecord增量filter_company_level匹配并将其写入文件

  2. 记录中的第二个过滤器针对value列表中的每个值self.keyword_material_info

    我。如果valuerecord增量filter_with_material_info匹配并将其写入文件

  3. 第三个过滤器适用于item中的self.keyword_bse_list

    我。如果itemrecord增量filter_with_keyword_info匹配,则将其写入文件

    II。如果存在最内部过滤器,现在转到下一条记录。

  4. 我已经编写了这些代码,它是否满足上述条件,还是有任何错误?他们都没有给出错误,但想确保逻辑是正确的。

            for record in fetch_record:
                total += 1
                for rec in cname:
                    try:
                        c_ngram = self.get_ngrams(rec['company_name'])
                        for gram in c_ngram:
                            if gram.lower()+' ' in u'{} {}'.format(record['title'], record['description']).lower():
                                filter_company_level += 1
                                # print "Matched based on company name : ", record['article_link']
                                company_write.write(record['article_link']+' - '+rec['company_name']+' - '+rec['company_code']+'\n')
                                for value in self.keyword_material_info:
                                    if value.lower()+' ' in u'{} {}'.format(record['title'], record['description']).lower():
                                        filter_with_material_info += 1
                                        materialinfo_write.write(record['article_link']+' - '+rec['company_name']+' - '+rec['company_code']+' - '+value+'\n')
                                        for item in self.keyword_bse_list:
                                            if item.lower()+' ' in u'{} {}'.format(record['title'], record['description']).lower():
                                                filter_with_keyword_info += 1
                                                keyword_write.write(record['article_link']+' - '+rec['company_name']+' - '+rec['company_code']+" - "+value+' - '+item+'\n')
                                                print record['article_link']
                                                print value
                                                print item
                                                break
                                        break
                                # break
                                raise GetOutOfLoop
                    except GetOutOfLoop:
                        break
    

    或者这个是正确的?

            for record in fetch_record:
                total += 1
                for rec in cname:
                    try:
                        c_ngram = self.get_ngrams(rec['company_name'])
                        for gram in c_ngram:
                            if gram.lower()+' ' in u'{} {}'.format(record['title'], record['description']).lower():
                                filter_company_level += 1
                                # print "Matched based on company name : ", record['article_link']
                                company_write.write(record['article_link']+' - '+rec['company_name']+' - '+rec['company_code']+'\n')
                                for value in self.keyword_material_info:
                                    if value.lower()+' ' in u'{} {}'.format(record['title'], record['description']).lower():
                                        filter_with_material_info += 1
                                        materialinfo_write.write(record['article_link']+' - '+rec['company_name']+' - '+rec['company_code']+' - '+value+'\n')
                                        flag_keyword = 0
                                        for item in self.keyword_bse_list:
                                            if item.lower()+' ' in u'{} {}'.format(record['title'], record['description']).lower():
                                                filter_with_keyword_info += 1
                                                keyword_write.write(record['article_link']+' - '+rec['company_name']+' - '+rec['company_code']+" - "+value+' - '+item+'\n')
                                                print record['article_link']
                                                print value
                                                print item
                                                flag_keyword = 1
                                                break
                                        if flag_keyword == 1:
                                            break
                                # break
                                if flag_keyword == 1:
                                    raise GetOutOfLoop
                    except GetOutOfLoop:
                        break
    

2 个答案:

答案 0 :(得分:2)

  

我已经编写了这些代码,它是否满足上述条件,还是有任何错误?他们都没有给出错误,但想确保逻辑是正确的。

确保最好的方法是编写单元测试,比较代码输出的代码输出的预期输出。

答案 1 :(得分:1)

你的意思是:

return

但是当你认为你必须求助于异常以摆脱嵌套循环时,我建议将嵌套循环分解为函数并使用def process_record(record): for rec in cname: for gram in c_ngram: if x: for value in self.keyword_material_info: if y: for item in self.keyword_bse_list: if z: return for record in fetch_record: process_record(record) 代替。像这样:

<string  name="app_url">http://example.com</string>