Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use itertools.chain instead of sum to concatenate lists #2212

Merged
merged 2 commits into from
Oct 5, 2018

Conversation

Stigjb
Copy link
Contributor

@Stigjb Stigjb commented Oct 4, 2018

The Python docs discourage sum for concatenating lists, and suggests itertools.chain instead[1]. I think this makes it clearer that the value is a list and not a number.

Also, the unit test for the evaluate_word_analogies function seemed to have been written for the deprecated accuracy function, where Word2Vec accuracy and KeyedVector accuracy may have been separate methods (?). It seemed pointless to compute analogies twice on the same model, so I changed the test to instead check important aspects of the return value from evaluate_word_analogies.

@menshikh-iv
Copy link
Contributor

menshikh-iv commented Oct 5, 2018

Hello @Stigjb, thanks for the PR.

Can you do the same changes for all sum(..., []) please, here are examples that I found (maybe somewhere else?)

gensim/models/keyedvectors.py:1147:            'correct': sum((s['correct'] for s in sections), []),
gensim/models/keyedvectors.py:1148:            'incorrect': sum((s['incorrect'] for s in sections), []),
gensim/models/keyedvectors.py:1253:            'correct': sum((s['correct'] for s in sections), []),
gensim/models/keyedvectors.py:1254:            'incorrect': sum((s['incorrect'] for s in sections), []),
gensim/models/wrappers/ldamallet.py:225:                tokens = sum(([self.id2word[tokenid]] * int(cnt) for tokenid, cnt in doc), [])
gensim/models/wrappers/ldamallet.py:227:                tokens = sum(([str(tokenid)] * int(cnt) for tokenid, cnt in doc), [])
gensim/test/test_corpora_dictionary.py:261:        all_tokens = sum(texts, [])

@menshikh-iv menshikh-iv changed the title use itertools.chain to concatenate lists Use itertools.chain instead of sum to concatenate lists Oct 5, 2018
@menshikh-iv
Copy link
Contributor

Thanks @Stigjb 👍 You are very active last time 🔥 take part in https://hacktoberfest.digitalocean.com/ (free T-shirts)

If you have time and want to help - have a look to #2059, I'll be really appreciated!

@menshikh-iv menshikh-iv merged commit 5934b13 into piskvorky:develop Oct 5, 2018
@Stigjb Stigjb deleted the concatenate-with-chain branch October 5, 2018 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants