If/else in python list comprehension
up vote
-1
down vote
favorite
I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like:
def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))
It returns the correct word from file based on passed argument in command line, but if the argument doesnt match, i want to return nothing, like ''. How could I add else to this statement?
python list-comprehension
add a comment |
up vote
-1
down vote
favorite
I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like:
def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))
It returns the correct word from file based on passed argument in command line, but if the argument doesnt match, i want to return nothing, like ''. How could I add else to this statement?
python list-comprehension
2
Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
– timgeb
Nov 10 at 22:48
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like:
def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))
It returns the correct word from file based on passed argument in command line, but if the argument doesnt match, i want to return nothing, like ''. How could I add else to this statement?
python list-comprehension
I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like:
def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))
It returns the correct word from file based on passed argument in command line, but if the argument doesnt match, i want to return nothing, like ''. How could I add else to this statement?
python list-comprehension
python list-comprehension
asked Nov 10 at 22:46
Frendom
266
266
2
Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
– timgeb
Nov 10 at 22:48
add a comment |
2
Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
– timgeb
Nov 10 at 22:48
2
2
Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
– timgeb
Nov 10 at 22:48
Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
– timgeb
Nov 10 at 22:48
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Should be:
def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))
The (... if ... else ...)
is actually the ternary operator and not part of the surrounding list comprehension.
EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15
@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Should be:
def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))
The (... if ... else ...)
is actually the ternary operator and not part of the surrounding list comprehension.
EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15
@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23
add a comment |
up vote
0
down vote
accepted
Should be:
def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))
The (... if ... else ...)
is actually the ternary operator and not part of the surrounding list comprehension.
EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15
@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Should be:
def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))
The (... if ... else ...)
is actually the ternary operator and not part of the surrounding list comprehension.
Should be:
def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))
The (... if ... else ...)
is actually the ternary operator and not part of the surrounding list comprehension.
edited Nov 10 at 22:55
answered Nov 10 at 22:50
Michael Butscher
3,93811320
3,93811320
EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15
@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23
add a comment |
EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15
@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23
EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15
EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15
@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23
@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244172%2fif-else-in-python-list-comprehension%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
– timgeb
Nov 10 at 22:48