python宽度_在Python中对齐框架宽度
发布日期:2021-06-24 07:11:16 浏览次数:5 分类:技术文章

本文共 733 字,大约阅读时间需要 2 分钟。

假设我们有一个单词列表,我们必须逐行将其框在一个矩形区域中。请参阅示例以更好地理解。

因此,如果输入类似于['hello','world','python','programming','nice'],则输出为***************

* hello       *

* world       *

* python      *

* programming *

* nice        *

***************

为了解决这个问题,我们将遵循以下步骤-l:=数组中最大字的长度

st:=放星星(l + 4)次

对于每个我说的话st:= st串联'*'串联我,然后添加大小(i的l-size的空间)串联'*'

st:=与st串联星(l + 4)次

返回st

让我们看下面的实现以更好地理解-

示例class Solution:

def solve(self, words):

l=max(len(x) for x in words)

st='*'*(l+4)+'\n'

for i in words:

st+='* '+i+' '*(l-len(i)+1)+'*'+'\n'

return st+'*'*(l+4)

ob = Solution()words = ['hello','world', 'python', 'programming','nice']

print(ob.solve(words))

输入值['hello','world', 'python', 'programming','nice']

输出结果***************

* hello       *

* world       *

* python      *

* programming *

* nice        *

***************

转载地址:https://blog.csdn.net/weixin_30909915/article/details/112989563 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:springboot实体类映射配置文件_SpringBoot配置文件自动映射到属性和实体类(8)
下一篇:python重载模块_Python importlib模块重载使用方法详解

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月17日 02时58分58秒