1

私はこのウェブサイトをきれいにして、すべての言葉を得ようとしています。しかし、ジェネレータを使用すると、リストを使用するよりも多くの言葉が得られます。また、これらの言葉は矛盾しています。時々私は1単語、時にはない、時には30単語以上を持っています。私は、Pythonドキュメントに関するジェネレータについて読んで、ジェネレータに関するいくつかの質問を探しました。私が理解していることは、違うべきではないということです。私はボンネットの下で何が起こっているのか理解していない。私はPython 3.6を使用しています。また、私はGenerator Comprehension different output from list comprehension?を読んだが、私は状況を理解することはできません。同じコードでは、リスト内包表記とジェネレータが異なるかどうかによって異なります。

これは、ジェネレータで最初に機能します。

def text_cleaner1(website): 
    ''' 
    This function just cleans up the raw html so that I can look at it. 
    Inputs: a URL to investigate 
    Outputs: Cleaned text only 
    ''' 
    try: 
     site = requests.get(url).text # Connect to the job posting 
    except: 
     return # Need this in case the website isn't there anymore or some other weird connection problem 

    soup_obj = BeautifulSoup(site, "lxml") # Get the html from the site 


    for script in soup_obj(["script", "style"]): 
     script.extract() # Remove these two elements from the BS4 object 

    text = soup_obj.get_text() # Get the text from this 

    lines = (line.strip() for line in text.splitlines()) # break into lines 

    print(type(lines)) 

    chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) # break multi-headlines into a line each 

    print(type(chunks)) 

    def chunk_space(chunk): 
     chunk_out = chunk + ' ' # Need to fix spacing issue 
     return chunk_out 

    text = ''.join(chunk_space(chunk) for chunk in chunks if chunk).encode('utf-8') # Get rid of all blank lines and ends of line 

    # Now clean out all of the unicode junk (this line works great!!!) 


    try: 
     text = text.decode('unicode_escape').encode('ascii', 'ignore') # Need this as some websites aren't formatted 
    except:               # in a way that this works, can occasionally throw 
     return               # an exception 

    text = str(text) 

    text = re.sub("[^a-zA-Z.+3]"," ", text) # Now get rid of any terms that aren't words (include 3 for d3.js) 
              # Also include + for C++ 


    text = text.lower().split() # Go to lower case and split them apart 


    stop_words = set(stopwords.words("english")) # Filter out any stop words 
    text = [w for w in text if not w in stop_words] 



    text = set(text) # Last, just get the set of these. Ignore counts (we are just looking at whether a term existed 
          # or not on the website) 

    return text 

これはリスト内包表記の第2の機能です。

def text_cleaner2(website): 
    ''' 
    This function just cleans up the raw html so that I can look at it. 
    Inputs: a URL to investigate 
    Outputs: Cleaned text only 
    ''' 
    try: 
     site = requests.get(url).text # Connect to the job posting 
    except: 
     return # Need this in case the website isn't there anymore or some other weird connection problem 

    soup_obj = BeautifulSoup(site, "lxml") # Get the html from the site 


    for script in soup_obj(["script", "style"]): 
     script.extract() # Remove these two elements from the BS4 object 

    text = soup_obj.get_text() # Get the text from this 

    lines = [line.strip() for line in text.splitlines()] # break into lines 

    chunks = [phrase.strip() for line in lines for phrase in line.split(" ")] # break multi-headlines into a line each 

    def chunk_space(chunk): 
     chunk_out = chunk + ' ' # Need to fix spacing issue 
     return chunk_out 

    text = ''.join(chunk_space(chunk) for chunk in chunks if chunk).encode('utf-8') # Get rid of all blank lines and ends of line 

    # Now clean out all of the unicode junk (this line works great!!!) 


    try: 
     text = text.decode('unicode_escape').encode('ascii', 'ignore') # Need this as some websites aren't formatted 
    except:               # in a way that this works, can occasionally throw 
     return               # an exception 

    text = str(text) 

    text = re.sub("[^a-zA-Z.+3]"," ", text) # Now get rid of any terms that aren't words (include 3 for d3.js) 
              # Also include + for C++ 


    text = text.lower().split() # Go to lower case and split them apart 


    stop_words = set(stopwords.words("english")) # Filter out any stop words 
    text = [w for w in text if not w in stop_words] 



    text = set(text) # Last, just get the set of these. Ignore counts (we are just looking at whether a term existed 
          # or not on the website) 

    return text 

このコードでは、ランダムに異なる結果が得られます。

text_cleaner1("https://www.indeed.com/rc/clk?jk=02ecc871f377f959&fccid=c46d0116f6e69eae") - text_cleaner2("https://www.indeed.com/rc/clk?jk=02ecc871f377f959&fccid=c46d0116f6e69eae") 
+0

私は数回コードを実行し、両方の機能に対して常に同じ結果を得ました。 '' .join() 'の中で' 'chunk_space(chunk)'を使用する代わりに – furas

+0

を使用すると、空の文字列の代わりにスペースを使用できます。 – furas

答えて

0

ジェネレータは"lazy"です。コードはすぐに実行されませんが、結果が必要になると後で実行されます。つまり、変数や関数からすぐに値を取得するのではなく、変数や関数への参照を保持します。ジェネレータでリンク

all_configs = [ 
    {'a': 1, 'b':3}, 
    {'a': 2, 'b':2} 
] 
unique_keys = ['a','b'] 


for x in zip(*([c[k] for k in unique_keys] for c in all_configs)): 
    print(x) 

print('---') 
for x in zip(*((c[k] for k in unique_keys) for c in all_configs)): 
    print(list(x)) 

から

例は、別forループ内forループがあります。

内部ジェネレータは、cの値の代わりにcへの参照を取得し、後で値を取得します。

後で(ジェネレータから結果を取得する必要がある場合)外部発電機for c in all_configsで実行を開始します。外部ジェネレータが実行されると、cの値ではなくcへの参照を使用する2つの内部ジェネクタが生成されますが、ループするとcの値も変更されます。

その後、最終的にcから値を得る内部ジェネレータを実行しますが、この時点でcにはすでに{'a': 2, 'b':2}があります。


ところで:あなたはtkinterのボタンでそれを使用するときlambdaと同様の問題がforループでもあります。

関連する問題