使用 Python 的格式规范迷你语言对齐浮点数 [英] Using Python's Format Specification Mini-Language to align floats

查看:66
本文介绍了使用 Python 的格式规范迷你语言对齐浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到我可以使用 Python 的 格式规范迷你语言来更好地控制字符串的方式显示.但是,我很难弄清楚如何使用它来显示在小数点上对齐的浮点数.

例如,假设我有以下三个列表:

job_IDs = ['13453', '123', '563456'];memory_used = [30, 150.54, 20.6];memory_units = ['MB', 'GB', 'MB'];

我想遍历这三个列表并打印

<块引用>

工作 13453:30 MB作业 123:150.54 MB作业 563456:20.6 GB

到目前为止我已经尝试过:

for i in range(len(jobIDs)):my_str = "{item:15}{value:6} {units:3}".format(item='Job' + job_IDs[i] + ':' , value=str(memories[i]),units=memory_units[i]);打印 my_str

打印:

<块引用>

工作 13453:30 MB作业 123:150.54 MB作业 563456:20.6 GB

这几乎是正确的,但它没有将小数点周围的浮点数对齐.我如何使用 Python 的 格式规范迷你语言来按照我需要的方式进行操作?

解决方案

这就是你想要的:

for i in range(len(job_IDs)):打印作业 {item:15} {value[0]:>6}.{value[1]:<6} {units:3}".format(item=job_IDs[i]+':', value=memory_used[i].split('.') if '.' in memory_used[i] else (memory_used[i], '0'), units=memory_units[i])

这是它的工作原理:

这是主要部分:value=memory_used[i].split('.') if '.'in memory_used[i] else (memory_used[i], '0'),意思是:如果有小数点,将字符串分割为整段和小数部分,或者设置小数部分为0.

然后在格式字符串中:{value[0]:>6}.{value[1]:<6} 的意思是,整个部分右移,后跟一个点,然后小数部分左移.

打印:

作业 13453:30.0 MB作业 123:150.54 GB作业 563456:20.6 MB

I read that I can use Python's Format Specification Mini-Language to have more control over how strings are displayed. However, I am having a hard time figuring out how to use it to display floats aligned on the decimal point.

For example, say I have thre following three lists:

job_IDs = ['13453', '123', '563456'];
memory_used = [30, 150.54, 20.6];
memory_units = ['MB', 'GB', 'MB'];

I would like to iterate through these three lists and print

Job 13453:   30      MB
Job 123:    150.54   MB
Job 563456:  20.6    GB

So far I have tried:

for i in range(len(jobIDs)):
   my_str = "{item:15}{value:6} {units:3}".format( 
   item='Job ' + job_IDs[i] + ':' , value=str(memories[i]),units=memory_units[i]);

   print my_str

which prints:

Job 13453:   30      MB
Job 123:     150.54  MB
Job 563456:  20.6    GB

which is almost right, but it does not align the floats around the decimal point. How can I use Python's Format Specification Mini-Language to do it the way I need?

解决方案

This is what you want:

for i in range(len(job_IDs)):
    print "Job {item:15} {value[0]:>6}.{value[1]:<6} {units:3}".format(item=job_IDs[i]+':', value=memory_used[i].split('.') if '.' in memory_used[i] else (memory_used[i], '0'), units=memory_units[i])

Here is how it works:

This is the main part: value=memory_used[i].split('.') if '.' in memory_used[i] else (memory_used[i], '0'), which means: if there is a decimal point, split the string as the whole and decimal part, or set the decimal part to 0.

Then in the format string: {value[0]:>6}.{value[1]:<6} means, the whole part shifted right, followed by a dot, then the decimal part shifted left.

which prints:

Job 13453:              30.0      MB
Job 123:               150.54     GB
Job 563456:             20.6      MB

这篇关于使用 Python 的格式规范迷你语言对齐浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆