Enayet Hussain
Enayet Hussain

Reputation: 1018

What is the difference between these two pieces of Python code?

What is the difference between these two pieces of code? The first one returns a list of tokens as expected but the second piece of code seems to just return a list with 1 item.

Variables

sentences = load_parsed_example_sentences() # list of sentences
# sentences are a list of BasicTokens
# BasicTokens are single words
aspects = ["plot"]

Code Sample 1:

for aspect in aspects:
    for sentence in sentences:
        aspect_tokens = sentence.get_query_tokens(aspect)
        print aspect_tokens[0]

Code Sample 2:

aspect_tokens = [aspect_token for aspect in aspects for sentence in sentences for aspect_token in sentence.get_query_tokens(aspect)]
print aspect_tokens[0]

Are these two not the same?

Upvotes: 3

Views: 107

Answers (4)

Batman
Batman

Reputation: 8917

They're not equivalent.
The first iterates over aspects and sentences, and creates a list of tokens at each step. Then it prints the first token in the list. But it does that once for each step.
The second creates a list, into which it puts each list that get__query_tokens produces. Then it prints a single set of token once, at the end.
I'm generally a big fan of list comprehensions, but I'd argue that in this case they should not be used because it's making the code harder to read.

Upvotes: 0

Billy
Billy

Reputation: 5609

Your first sample prints once per each sentence per each aspect, so you'll get a total of len(aspects) * len(sentences) prints (assuming aspects and sentences are lists). Your second sample only has one print, so it will only print one element.

Do you expect aspect_tokens to be the same after each sample executes? Because they won't be. The first sample reassigns aspect_tokens in each iteration, so after the loops execute, aspect_tokens will be equal to the last computed value, specifically, sentences[-1].get_query_tokens(aspects[-1]).

I'm deciphering what you're after might be the following:

Code Sample 1

aspect_tokens = []
for aspect in aspects:
    for sentence in sentences:
        all_aspect_tokens.append(sentence.get_query_tokens(aspect)[0])

Code Sample 2

aspect_tokens = [sentence.get_query_tokens(aspect)[0] for sentence in sentences for aspect in aspects]

These two samples will result in identical aspect_tokens lists.

Upvotes: 1

RChanger
RChanger

Reputation: 110

aspect_tokens = [sentence.get_query_tokens(aspect)[0] for aspect in aspects for sentence in sentences]
print aspect_tokens

Upvotes: 0

Ébe Isaac
Ébe Isaac

Reputation: 12321

Both code segments are different.

In the first one, you are calling the sentence.get_query_tokens(aspect) and printing the first instance of the aspect token obtained.

In the second one, you are using the list builder construct to build an array of aspect tokens and printing out the first set of tokens.

Upvotes: 2

Related Questions