On Sun, May 02, 2021 at 04:09:21AM -0000, Valentin Berlier wrote:
> Let's say i have a matrix of numbers:
>
> matrix = [[randint(7, 50) / randint(1, 3) for _ in range(4)] for _ in
> range(4)]
>
> I want to format and display each row so that the columns are nicely lined
> up. Maybe also display the sum of the row at the end of each line:
>
> for row in matrix:
> print(''.join(f'{n:>8.3f}' for n in row) + f' | {sum(row):>8.3f}')
>
> This gives me a nicely formatted table. Now with the proposal:
>
> for row in matrix:
> print(f'{n for n in row:>8.3f} | {sum(row):>8.3f}')
As a general rule, we should avoid needless generator expressions that
just iterate over themselves:
n for n in row
is just the same as
row
except it creates a pointless generator to iterate over something that
is already iterable.
Your proposed f-string syntax:
f'{n for n in row:>8.3f} | {sum(row):>8.3f}'
is already legal except for the first format specifier. Removing that:
f'{n for n in row} | {sum(row):>8.3f}'
gives us working code. I don't believe that we ought to confuse the
format specifier syntax by making the f format code have magical powers
when given an in-place generator expression, and otherwise have the
regular meaning for anything else.
Better to invent a new format code, which I'm going to spell as 'X' for
lack of something better, that maps a format specifier to every
element of any iterable (not just generator comprehensions):
f'{row:X>8.3f} | {sum(row):>8.3f}'
meaning, format each element of row with `>8.3f`.
> The idea is that you would be able to embed a comprehension in
> f-string interpolations,
When the only tool you have is a hammer, every problem looks like it is
crying out for a comprehension *wink*
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/QI4YOVJWWMDBPXUG2MGVCKAWHKEV4VSM/
Code of Conduct: http://python.org/psf/codeofconduct/