3
I have a list that represents 5 one-month periods with 31 days, and each period contains daily target values for a retail seller. Only for elucidation purposes would the empty structure be like this:
[[], [], [], [], []]
ITERATING OVER THE LIST (WITH VALUES):
for periodo in plan:
print(periodo)
OUTPUT:
[2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0]
[2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0]
[2000.0, 2000.0, 1250.0, 1250.0, 1250.0, 1250.0, 1250.0]
[1250.0, 1250.0, 1250.0, 1250.0, 1250.0, 1250.0, 1250.0]
[1250.0, 1250.0, 1250.0]
The periods did not become equal because there is a cut on Sundays.
I want to plot these values in a modular way, with a horizontal header that discriminates each period like this:
print(f'\033[34m{"1º PERÍODO":<15}{"2º PERÍODO":<15}{"3º PERÍODO":<15}{"4º PERÍODO":<15}{"5º PERÍODO":<15}\033[m')
OUTPUT:
1º PERÍODO 2º PERÍODO 3º PERÍODO 4º PERÍODO 5º PERÍODO
My difficulty is to have each element of the planning list (periods) organized horizontally according to the header. I tried a solution with geradores
, among others, but it just doesn’t work.
My first attempt with generators was this:
gerador_periodo = (periodo for periodo in plan)
print(f"{next(gerador_periodo)}, {next(gerador_periodo)}, {next(gerador_periodo)}, {next(gerador_periodo)}, {next(gerador_periodo)}")
Obviously, it failed, because it printed the periods in a linear way, not respecting the organization of the header above.
My second attempt was using a list comprehension
looped for
to print breaking lines:
print(f"{[print(num) for num in next(gerador_periodo)]}, {[print(num) for num in next(gerador_periodo)]}, ...")
Here, I received on the screen all values below each other followed by the 5 periods with values None
(Why? ). I tried using the parameter end
in various ways also.
Is it possible to build this structure without using Pandas, Dataframe, etc? Some other idea?
@hksotsubo, helped a lot! The size having to be assigned to size is because of the interpolation within another interpolation, right?
– Curi
Another interesting thing was that I couldn’t format the string with 2 floating houses (in case I needed to). I had to make this change directly into the other function that provides the planning for this modeling function that you created with me.
– Curi
@dev.Uri O
size
is because inside the string used informat
, it has to know what to put there (detail that would not need to have the same name, unlike an f-string, which has to be the same variable name). To always leave 2 decimal places, just use{:<{size}.2f}
. But as in this case there are empty strings between the values, it will give error pq this format always expects numbers. So a solution would be to format outside theprint
. Example: https://ideone.com/Ud3LTW– hkotsubo
Perfect. As I said, plan is a list generated by another function called planner that appends values in a for loop. I append an f-string formatted with . 2f to already receive the values this way in the tabular function. The problem was when calculating the sum of periods to put below each column to make it even more complete. I made a conversion to float in-place and calculated the sum of each period with sum().
– Curi
I have 5 months of Python and the challenge is always to understand which is the best way, the most pythonic. Gradually this becomes clearer. In your opinion, the path I have set is a good one?
– Curi
When you say format always expects numbers you mean what? Size in case is an integer. It has to be an even number?!
– Curi
@dev.Curi Yes, the size there is the amount of spaces that will be used, so it only makes sense if it is an integer number. Look at this - and many more options - in the documentation: https://docs.python.org/3/library/string.html#formatspec - but what I meant is that the format
.2f
only works with numbers (because if it’s not a number it won’t have decimals and this format says to print only 2 houses), but as it also has the empty string in some cases (in the columns that are empty), so it doesn’t work, so I did the formatting in a separate function– hkotsubo