Posts: 65
Threads: 18
Joined: Nov 2016
Jan-18-2017, 05:54 PM
(This post was last modified: Jan-18-2017, 05:54 PM by birdieman.)
Assuming each variable is to be formatted the same way, is there a way to avoid coding '${:,.0f}' in front of every variable that is to be printed on a line?
print(hdr62.format(
'${:,.0f}'.format(ETE).rjust(12, ' '),
'${:,.0f}'.format(ETFI).rjust(12, ' '),
'${:,.0f}'.format(ETC).rjust(12, ' '),
'${:,.0f}'.format(ETTA).rjust(12, ' '),
'${:,.0f}'.format(ETFE).rjust(12, ' '),
'${:,.0f}'.format(ETFFI).rjust(12, ' '),
'${:,.0f}'.format(ETFC).rjust(12, ' '))) by the way, hdr62 is just a spacing format, like '{:>13}'
thanks
Posts: 2,342
Threads: 62
Joined: Sep 2016
The code
('${:,.0f}'.format(ETE).rjust(12, ' '),
'${:,.0f}'.format(ETFI).rjust(12, ' '),
'${:,.0f}'.format(ETC).rjust(12, ' '),
'${:,.0f}'.format(ETTA).rjust(12, ' '),
'${:,.0f}'.format(ETFE).rjust(12, ' '),
'${:,.0f}'.format(ETFFI).rjust(12, ' '),
'${:,.0f}'.format(ETFC).rjust(12, ' ')) can be compressed to
tuple('${:,.0f}'.format(num).rjust(12, ' ') for num in nums) assuming that all those numbers are in an ordered collection called nums. You can use unpacking to pass the tuple generated with the above comprehension to hdr62.format().
Posts: 2,953
Threads: 48
Joined: Sep 2016
This is not going to work. The {} represent the position in the formatted string for every argument in format().
'{}, {}, {}, {}, {}'.format('one', 'two', three', 'four', 'five')
Posts: 65
Threads: 18
Joined: Nov 2016
Jan-18-2017, 07:40 PM
(This post was last modified: Jan-18-2017, 07:42 PM by birdieman.)
As a beginner, I tried this example:
w = 45667.778
x = 56785.55
y = 34529.4568
z = 789612.554
nums = (w, x, y, z)
print(tuple('${:,.0f}'.format(num).rjust(12, ' ') for num in nums)) and got:
(' $45,668', ' $56,786', ' $34,529', ' $789,613') 1. Is that the right way to do it?
2. How do I get rid of the ( ) ' ' and the , between the numbers?
thanks for responding
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jan-18-2017, 07:46 PM
(This post was last modified: Jan-18-2017, 07:49 PM by wavic.)
I mean your first post. You have seven variables but not seven '{}' to print them.
This part:
print(hdr62.format(
'${:,.0f}'.format(ETE).rjust(12, ' '),
'${:,.0f}'.format(ETFI).rjust(12, ' '),
'${:,.0f}'.format(ETC).rjust(12, ' '),
'${:,.0f}'.format(ETTA).rjust(12, ' '),
'${:,.0f}'.format(ETFE).rjust(12, ' '),
'${:,.0f}'.format(ETFFI).rjust(12, ' '),
'${:,.0f}'.format(ETFC).rjust(12, ' '))) hdr62 holds one '{}' but the format has seven arguments.
In [1]: print('{:>13}'.format(
...: '${:,.0f}'.format(1).rjust(12, ' '),
...: '${:,.0f}'.format(2).rjust(12, ' '),
...: '${:,.0f}'.format(3).rjust(12, ' '),
...: '${:,.0f}'.format(4).rjust(12, ' '),
...: '${:,.0f}'.format(5).rjust(12, ' '),
...: '${:,.0f}'.format(6).rjust(12, ' '),
...: '${:,.0f}'.format(7).rjust(12, ' ')))
$1
In [2]:
Posts: 65
Threads: 18
Joined: Nov 2016
Jan-18-2017, 07:48 PM
(This post was last modified: Jan-18-2017, 07:49 PM by birdieman.)
No, hdr62 has seven arguments, I just did not list them all -- for brevity
Posts: 2,342
Threads: 62
Joined: Sep 2016
wavic, do you know the contents of the hdr62.format() code? I feel like you're speaking to a different situation.
(Jan-18-2017, 07:40 PM)birdieman Wrote: 1. Is that the right way to do it? It's the right way to simplify that subset of your original code. As I said, to integrate it fully you'll need argument unpacking.
(Jan-18-2017, 07:40 PM)birdieman Wrote: 2. How do I get rid of the ( ) ' ' and the , between the numbers? That'll happen automatically as part of the unpacking.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jan-18-2017, 07:51 PM
(This post was last modified: Jan-18-2017, 08:07 PM by wavic.)
(Jan-18-2017, 07:48 PM)birdieman Wrote: No, hdr62 has seven arguments, I just did not list them all -- for brevity
Its clear now
Posts: 65
Threads: 18
Joined: Nov 2016
Thanks - I will read up on how to "unpack"
Posts: 65
Threads: 18
Joined: Nov 2016
I have looked and read about unpacking tuples, and found several examples, but none of the examples refers to printing. I am too much of a beginner to translate those examples into what I need, which micseydel alluded to above: Unpack the tuple, and print all variables using the format hdr62 -- all in one line of code. Here is what I have:
w = 45667.778
x = 56785.55
y = 34529.4568
z = 789612.554
nums = (w, x, y, z)
hdr62 = '{:>15} {:>15} {:>15} {:>15}'
print(tuple('${:,.0f}'.format(num).rjust(12, ' ') for num in nums)) Can someone give me further help? Thanks
|