I'm quite new to Python, did search here for this question and can't find similar question so far.
I have a set of conditions for a list comprehension which will vary, and if possible, I thought to try concatenate the conditions as a string, and use this string variable inside the list comprehension:
The following works fine:
Total = sum(sale["Date"] == date for sale in Sales_Store)
Where date is an integer, and Sales_Store a list of dictionaries with same keys, "Date" is one of the key names. it returns the number of sales in the sales store which satisfy this condition.
But this gives error: unsupported operand type(s) for +: 'int' and 'str'
strTest = "sale[\"Date\"] == date for sale in Sales_Store"
Total = sum(strTest)
Of course, I only want this condition in the list comp. if certain other condition holds true, and have several other conditions to concatenate with this, each condition only applying in certain circumstances.
So do you know if there is way to make this concatenation work?
Aucun commentaire:
Enregistrer un commentaire