我有一个非常长的字符串列表,我想知道是否有任何方法可以通过一定数量的单词(如底部的图像)来分隔每个字符串的元素
List = [“End extreme poverty in all forms by 2030”,
“End hunger, achieve food security and improved nutrition and promote sustainable agriculture”,
“Ensure healthy lives and promote well-being for all at all ages”,
“Ensure inclusive and equitable quality education and promote lifelong learning opportunities for all”,
“Promote sustained, inclusive and sustainable economic growth, full and productive employment and decent work for all”]
Print(list)
Out:
“End extreme poverty in all forms by 2030”,
“End hunger, achieve food security and improved
nutrition and promote sustainable agriculture”,
“Ensure healthy lives and promote well-being for all at all ages”,
“Ensure inclusive and equitable quality education and promote
lifelong learning opportunities for all”,
“Promote sustained, inclusive and sustainable economic growth,
full and productive employment and decent work for all”]
最终的结果有点像图像
您可以使用
textwrap
:如果你想在字数上打破它,你可以:
将字符串拆分为单词列表:
然后可以使用numpy将其分解为K个块:
然后,要将块恢复为字符串,可以执行以下操作:
或者,要将它们全部转换为字符串,可以执行以下操作:
相关问题 更多 >
编程相关推荐