2017-10-03 9 views
0

この関数の可読性を向上させるために、ネストされたforループをどのように使うべきかと思います。おそらくtags_files =nested for loopを使用できますか?この機能を改善するためにネストされたforループを使用するにはどうすればよいですか?

def search_repotag_for_file(search_filepath, repo): 
    '''Goes through all tags, all files to find a github file entity 
     which matches the search_filepath we are looking for''' 
    all_tags = (tag for tag in repo.tags) 
    tags_files = ((tag, file_ent) for tag in all_tags for file_ent in tag.commit.tree.traverse()) 
    matches = (tup for tup in tags_files if tup[1].path == search_filepath) 
    return matches 

答えて

5

私はあなたのために機能を書き換えてみましょう:あなたが探している

def search_repotag_for_file(search_filepath, repo): 
    for tag in repo.tags: 
     for file_ent in tag.commit.tree.traverse(): 
      if file_ent.path == search_filepath: 
       yield (tag, file_ent) 

キーワードはyield

+0

すごいです!これは完璧です。私はこの答えに近づきましたが、正確ではありませんでした。ありがとう! :D – PandasRocks

関連する問題