Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中断开长字符串?_Python_String_List_Matplotlib - Fatal编程技术网

如何在python中断开长字符串?

如何在python中断开长字符串?,python,string,list,matplotlib,Python,String,List,Matplotlib,我有一个非常长的字符串列表,我想知道是否有任何方法可以通过一定数量的单词(如底部的图像)来分隔每个字符串的元素 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 f

我有一个非常长的字符串列表,我想知道是否有任何方法可以通过一定数量的单词(如底部的图像)来分隔每个字符串的元素

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”]
最终的结果有点像图像


如果您想在字数方面打破它,您可以:

将字符串拆分为单词列表:

my_list = my_string.split()
然后可以使用numpy将其分解为K个块:

import numpy as np
my_chunks = np.array_split(my_list , K) #K is number of chunks
然后,要将块恢复为字符串,可以执行以下操作:

my_string = " ".join(my_chunk[0])
my_strings = list(map(" ".join, my_chunks))
或者,要将它们全部转换为字符串,可以执行以下操作:

my_string = " ".join(my_chunk[0])
my_strings = list(map(" ".join, my_chunks))

如果你想在字数上打破它,你可以:

将字符串拆分为单词列表:

my_list = my_string.split()
然后可以使用numpy将其分解为K个块:

import numpy as np
my_chunks = np.array_split(my_list , K) #K is number of chunks
然后,要将块恢复为字符串,可以执行以下操作:

my_string = " ".join(my_chunk[0])
my_strings = list(map(" ".join, my_chunks))
或者,要将它们全部转换为字符串,可以执行以下操作:

my_string = " ".join(my_chunk[0])
my_strings = list(map(" ".join, my_chunks))

您可以使用
textwrap

import textwrap
print(textwrap.fill(List[1], 50))


您可以使用
textwrap

import textwrap
print(textwrap.fill(List[1], 50))


转换整个列表:
list=[textwrap.fill(l,50)表示列表中的l]
转换整个列表:
list=[textwrap.fill(l,50)表示列表中的l]
这是否回答了您的问题?这回答了你的问题吗?