简体   繁体   English

如何在列表中使用字符串格式?

[英]How to use string formatting within a list?

Is it possible to use the string formatting method within a list?是否可以在列表中使用字符串格式化方法?

For example例如

list1 = ["{0:^8}", "{1:^8}", "{2:^8}".format(7, 8, 9)]

But whenever I try to run it it gives the output as only having changed the last one.但是每当我尝试运行它时,它都会给出输出,因为它只更改了最后一个。

['{0:^8}', '{1:^8}', '   9    ']

How to format the complete list?如何格式化完整列表?

You're only formatting the last string.您只是格式化最后一个字符串。 You just need to loop over the numbers, and since the format spec is the same for all of them, you can reuse it*.您只需要遍历数字,并且由于所有数字的格式规范都相同,因此您可以重复使用它*。

>>> ['{:^8}'.format(x) for x in (7, 8, 9)]
['   7    ', '   8    ', '   9    ']

* As opposed to a different spec for each one, for which you could use zip , like * 与每个不同的规范相反,您可以使用zip ,例如

[spec.format(x) for spec, x in zip(specs, numbers)]

You're only calling str.format on one string in the list.您只是在列表中的一个字符串上调用str.format Your code is working properly, not in the way you want, but in the way you coded it.你的代码工作正常,不是按照你想要的方式,而是按照你编码的方式。

So there's really only 2 clear ways to do this imo.所以实际上只有两种明确的方法可以做到这一点。

Your values are 7, 8, 9 let's store them into a variable.您的值为7, 8, 9让我们将它们存储到一个变量中。 Then we can use map on them to data which we imply are the formats for each string:然后我们可以对它们使用 map 到数据,我们暗示是每个字符串的格式:

>>> vals = 7, 8, 9
>>> data = ["{:^8}", "{:^8}", "{:^8}"]
>>> list(map(str.format, data, vals))
['   7    ', '   8    ', '   9    ']

Or using f-string s without implying data first, as all the formatting is the same for each value:或者使用f-string而不先暗示data ,因为每个值的所有格式都是相同的:

>>> vals = 7, 8, 9
>>> [f'{v:^8}' for v in vals]

For an alternative I guess you could use str.format in a list comprehension as well but this isn't as clean or fancy as f-string s:对于替代方案,我想您也可以在列表理解中使用str.format ,但这不像f-string s 那样干净或花哨:

>>> vals = 7, 8, 9
>>> ['{:^8}'.format(v) for v in vals]

The format function works on only one string. format函数只对一个字符串起作用。 So if you have multiple strings then you need to call the format method on each of them either through a loop or individually on each one of them.因此,如果您有多个字符串,那么您需要通过循环或单独对每个字符串调用 format 方法。 "{} {} {}".format(7,8,9) works as it is one complete string, but "","","{}".format(7,8,9) applies the last value to that place holder in the last string as it is only called on it. "{} {} {}".format(7,8,9)是一个完整的字符串,但"","","{}".format(7,8,9)将最后一个值应用于最后一个字符串中的占位符,因为它只被调用。

You could use f-strings with list comprehension to get the output -您可以使用带有列表理解的f-strings来获得输出 -

output = [f'{i:^8}' for i in [7, 8, 9]]
print(output)

In your code, the .format is for the last element, so only that will be formatted.在您的代码中, .format用于最后一个元素,因此只会对其进行格式化。

However, here, all the items are formatted, then added to the list.但是,在这里,所有项目都被格式化,然后添加到列表中。

This is a simple fix.这是一个简单的修复。 You are expecting to format all elements individually which would require separate format calls.您希望单独格式化所有元素,这需要单独的格式调用。 In reality you want to separate the string elements as their own elements in the list using commas for each item enclosed in quotations.实际上,您希望将字符串元素作为它们自己的元素在列表中使用逗号分隔在引号中的每个项目。 This way formatting can be done on each item.这种方式可以对每个项目进行格式化。

# You need to adjust your string quotations like this
list1=["{0:^8},{1:^8},{2:^8}".format(7,8,9)]

# Check the results of the adjustment
for item in list1:
    print(item)

list1=["{0:^8}","{1:^8}","{2:^8}"]    #Insert special characters into a string
str1='|'.join(list1)
list2=str1.format(7,8,9).split('|')
print(list2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM