设置列表中所有元素的格式

发布于 2024-08-31 08:19:57 字数 344 浏览 4 评论 0原文

我想打印一个数字列表,但我想在打印之前格式化列表中的每个成员。 例如,

theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]

我希望将上述列表作为输入打印以下输出:

[1.34, 7.42, 6.97, 4.55]

对于列表中的任何一个成员,我知道我可以使用以下命令对其进行格式化:

print "%.2f" % member

是否有命令/函数可以对整个列表执行此操作?我可以写一个,但想知道是否已经存在。

I want to print a list of numbers, but I want to format each member of the list before it is printed.
For example,

theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]

I want the following output printed given the above list as an input:

[1.34, 7.42, 6.97, 4.55]

For any one member of the list, I know I can format it by using

print "%.2f" % member

Is there a command/function that can do this for the whole list? I can write one, but was wondering if one already exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

猫卆 2024-09-07 08:19:57

如果您只想打印数字,您可以使用一个简单的循环:

for member in theList:
    print "%.2f" % member

如果您想存储结果供以后使用,您可以使用列表理解:

formattedList = ["%.2f" % member for member in theList]

然后您可以打印此列表以获取问题中的输出:

print formattedList

另请注意 < code>% 已被弃用。如果您使用的是 Python 2.6 或更高版本,则更喜欢使用 format

If you just want to print the numbers you can use a simple loop:

for member in theList:
    print "%.2f" % member

If you want to store the result for later you can use a list comprehension:

formattedList = ["%.2f" % member for member in theList]

You can then print this list to get the output as in your question:

print formattedList

Note also that % is being deprecated. If you are using Python 2.6 or newer prefer to use format.

无戏配角 2024-09-07 08:19:57

使用 "".format() 和生成器表达式的非常简短的解决方案:

>>> theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]

>>> print(['{:.2f}'.format(item) for item in theList])

['1.34', '7.42', '6.97', '4.55']

A very short solution using "".format() and a generator expression:

>>> theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]

>>> print(['{:.2f}'.format(item) for item in theList])

['1.34', '7.42', '6.97', '4.55']
挽清梦 2024-09-07 08:19:57

对于Python 3.5.1,您可以使用:

>>> theList = [1.343465432, 7.423334343, 6.967997797, 4.5522577]
>>> strFormat = len(theList) * '{:10f} '
>>> formattedList = strFormat.format(*theList)
>>> print(formattedList)

结果是:

'  1.343465   7.423334   6.967998   4.552258 '

For Python 3.5.1, you can use:

>>> theList = [1.343465432, 7.423334343, 6.967997797, 4.5522577]
>>> strFormat = len(theList) * '{:10f} '
>>> formattedList = strFormat.format(*theList)
>>> print(formattedList)

The result is:

'  1.343465   7.423334   6.967998   4.552258 '
时间你老了 2024-09-07 08:19:57

您可以使用列表理解、连接和一些字符串操作,如下所示:

>>> theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]
>>> def format(l):
...     return "["+", ".join(["%.2f" % x for x in l])+"]"
... 
>>> format(theList)
'[1.34, 7.42, 6.97, 4.55]'

You can use list comprehension, join and some string manipulation, as follows:

>>> theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]
>>> def format(l):
...     return "["+", ".join(["%.2f" % x for x in l])+"]"
... 
>>> format(theList)
'[1.34, 7.42, 6.97, 4.55]'
放血 2024-09-07 08:19:57

您可以使用地图功能

l2 = map(lambda n: "%.2f" % n, l)

You can use the map function

l2 = map(lambda n: "%.2f" % n, l)
凯凯我们等你回来 2024-09-07 08:19:57

如果您不需要保存您的值,请尝试这个:

list = [0.34555, 0.2323456, 0.6234232, 0.45234234]
for member in list:
    form='{:.1%}'.format(member)
    print(form)

输出:

34.6%
23.2%
62.3%
45.2%

Try this one if you don't need to save your values:

list = [0.34555, 0.2323456, 0.6234232, 0.45234234]
for member in list:
    form='{:.1%}'.format(member)
    print(form)

output:

34.6%
23.2%
62.3%
45.2%
乖乖兔^ω^ 2024-09-07 08:19:57

您可以使用辅助函数:

def format_list(a, fmt, sep=', ', start='[', end=']'):
    return start + sep.join([format(x, fmt) for x in a]) + end

用法:

a=[4,8,15,16,23,42]

print(f"%d is {format_list(a, 'd')} %x is {format_list(a, 'x')} %b is {format_list(a, 'b')}")

You can use helper function:

def format_list(a, fmt, sep=', ', start='[', end=']'):
    return start + sep.join([format(x, fmt) for x in a]) + end

usage:

a=[4,8,15,16,23,42]

print(f"%d is {format_list(a, 'd')} %x is {format_list(a, 'x')} %b is {format_list(a, 'b')}")
逆蝶 2024-09-07 08:19:57

您还可以使用 f 字符串和列表理解:

 print([f'{item: .2f}' for item in theList])

You can also use f-strings and a list comprehension:

 print([f'{item: .2f}' for item in theList])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文