2
Here is a practical example of a script that simulates a seller’s sales days and extracts only the days when the seller hit the day’s quota.
The list represents the month (I entered only 3 days for practical example) and the dictionaries the days themselves, with sales values and KPI’s. In this script I used the simplest solution with a loop For
, see:
vendas = [{"Dia": '1', "Cota": 3200, "Venda": 2500, "PA": 2, "VM": 1452},
{"Dia": '2', "Cota": 3200, "Venda": 1803, "PA": 2.4, "VM": 1452},
{"Dia": '3', "Cota": 5000, "Venda": 8500, "PA": 1.82, "VM": 1385}]
# Objetivo é gerar uma lista com os dias em que o vendedor bateu a cota
print('O vendedor bateu cota nos dias...', end=' ')
for c, dia in enumerate(vendas):
if dia["Venda"] >= dia["Cota"]:
print(dia["Dia"], end= ', ')
Easy right?
Now, what if I want to wear one lambda
with filter
to extract exactly the same result? Intuitively, this was the code I wrote and is not compiling:
# Com filter imprimindo a lista inteira de uma vez
print('O vendedor bateu cota nos dias...', end=' ')
print(list(filter(lambda x['Dia']: x['Venda'] >= x['Cota'], vendas)))
Can you help me build this solution with this feature? Is it possible? It would be nice to build the solution with List Comprehension tb because I could not. Thank you!
Perfect! Another question: why do we have to flag with list() before printing the result? Map does not automatically generate a list object?
– Curi
@dev.Uri, no, the function
map
returns an instance ofmap
. You can iterate on instances ofmap
with a bowfor
, butmap
is not alist
; cannot add more items to amap
, and if you print it directly on the console will appear onlymap object at [endereço de memória]
– Andre
nice @user140828. So to visualize the instance I should assign an object that is printable (roughly speaking), is that it? In the case of this example I could for example play inside a tuple, because I won’t add or change that instance. PS. I can only iterate under the instance of this map() if I sort it, right?
– Curi