Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Streamline this format code
#11
I fixed it. This is what I used (in the above example):
nums2 = tuple('${:,.0f}'.format(num).rjust(12, ' ') for num in nums)
print(hdr62.format(*nums2))
Is that solution efficient?

Another solution, but better (I think) because no need to make another tuple:
print(hdr62.format(*['${:,.0f}'.format(n) for n in nums]))
Reply
#12
Yes, it's reasonably efficient. I think you can do better though, since your hdr62 format string is very redundant. Something like this
hdr63 = "{:>15}"
' '.join(hdr63.format('${:,.0f}'.format(n)) for n in nums)
though I believe that you can get this into one format. I haven't tested it, but it would probably be something like this
' '.join('${:>,15.0f}'.format(n) for n in nums)
The > symbol might need to be placed somewhere else though.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 656 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Please suggest python code to format DNA sequence FASTA file rajamdade 4 3,117 Oct-24-2019, 04:36 AM
Last Post: rajamdade
  code help to convert log file to csv format bmunagala 3 12,088 Jan-21-2019, 06:08 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020