If/else in python list comprehension

2024/11/18 1:47:06

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?

Answer

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.

https://en.xdnf.cn/q/118736.html

Related Q&A

Conditional module importing in Python

Im just trying out Maya 2017 and seen that theyve gone over to PySide2, which is great but all of my tools have import PySide or from PySide import ... in them.The obvious solution would be to find/rep…

AttributeError: list object has no attribute lower : clustering

Im trying to do a clustering. Im doing with pandas and sklearn. import pandas import pprint import pandas as pd from sklearn.cluster import KMeans from sklearn.metrics import adjusted_rand_score from s…

I’m dealing with an error when I run server in Django

PS C:\Users\besho\OneDrive\Desktop\DjangoCrushcourse> python manage.py runserver C:\Users\besho\AppData\Local\Programs\Python\Python312\python.exe: cant open file C:\Users\besho\OneDrive\Desktop\Dja…

python threading with global variables

i encountered a problem when write python threading code, that i wrote some workers threading classes, they all import a global file like sharevar.py, i need a variable like regdevid to keep tracking t…

How to write nth value of list into csv file

i have the following list : sec_min=[37, 01, 37, 02, 37, 03, 37, 04, 37, 05,....]i want to store this list into CVS file in following format: 37,01 37,02 37,03 37,04 ... and so onthis is what i coded: …

Read R function output as columns

Im trying to come up with a way to solve this question I asked yesterday:rpy2 fails to import rgl R packageMy goal is to check if certain packages are installed inside R from within python.Following th…

How does .split() work? - Python

In the following examples, I am splitting an empty string by a space. However, in the first example I explicitly used a space and in the second example, I didnt. My understanding was that .split() and …

How to get email.Header.decode_header to work with non-ASCII characters?

Im borrowing the following code to parse email headers, and additionally to add a header further down the line. Admittedly, I dont fully understand the reason for all the scaffolding around what should…

Elif syntax error in Python

This is my code for a if/elif/else conditional for a text-based adventure game Im working on in Python. The goal of this section is to give the player options on what to do, but it says there is someth…

Convert date from dd-mm-yy to dd-mm-yyyy using python [duplicate]

This question already has answers here:How to parse string dates with 2-digit year?(6 answers)Closed 7 years ago.I have a date input date_dob which is 20-Apr-53 I tried converting this to format yyyy…