删除pandas和numpy中句子列表中的单个字符单词

删除pandas和numpy中句子列表中的单个字符单词,pandas,list,pandas-groupby,Pandas,List,Pandas Groupby,我有一个句子列表,如下所示 list_x = ['a b c d e f g', 'abc def ghi', 'ab c d e f gh i j', ' ab cd ef gh'] 其中每个由空格分隔的字符串被视为单词 从上面的列表中,我想删除单字符单词,并用“-”替换空格 预期产出: x5 = ['abc-def-ghi' , 'ab-cd-ef-gh' ] 我尝试了以下方法: for item in list_x: if (sum(len(x) == 1 for x in item.

我有一个句子列表,如下所示

list_x = ['a b c d e f g', 'abc def ghi', 'ab c d e f gh i j', ' ab cd ef gh']
其中每个由空格分隔的字符串被视为单词

从上面的列表中,我想删除单字符单词,并用“-”替换空格

预期产出:

x5 = ['abc-def-ghi' , 'ab-cd-ef-gh' ]
我尝试了以下方法:

for item in list_x:
if (sum(len(x) == 1 for x in item.split(" "))) < 5:
x5.append(item.replace(' ','-'))
列表中项目的
:
如果(拆分项目(“”)中x的总和(len(x)=1)小于5:
x5.追加(项目.替换('''-'))

我想知道有没有更快的方法来完成上述任务。

一个想法是在此处使用列表理解,如果可能,添加尾随空格
strip

list_x = ['a b c d e f g', 'abc def ghi', 'ab c d e f gh i j', ' ab cd ef gh']

L = [item.strip().replace(' ','-') 
     for item in list_x 
     if (sum(len(x) == 1 for x in item.split())) < 5]
print (L)
['abc-def-ghi', 'ab-cd-ef-gh']

L = [item.replace(' ','-') 
     for item in list_x 
     if (sum(len(x) == 1 for x in item.split())) < 5]
print (L)
['abc-def-ghi', '-ab-cd-ef-gh']
list_x=['ab c d e f g','abc def ghi','ab c d e f gh i j','ab cd ef gh']
L=[item.strip().replace('',-'))
对于列表_x中的项目
如果(项拆分()中x的总和(len(x)=1)<5]
印刷品(L)
['abc-def-ghi','ab-cd-ef-gh']
L=[项目.替换('',-'))
对于列表_x中的项目
如果(项拆分()中x的总和(len(x)=1)<5]
印刷品(L)
['abc-def-ghi','-ab cd ef gh']

它是由列表填充的列?不,它只是上面输出的一个列表,如何避免拖尾'-'@ALI-使用第一个解决方案,在
[item.strip().replace('''-')中添加了
.strip()