从列表中的句子中删除单字母词

问题描述 投票:0回答:1

我有以下列表。

ip= ['a boy called me z there', 'what u doing b over there ', "come w me t the end']

我想把列表中的每一个字符串中的单字母都去掉

我尝试了以下方法,但没有成功。

x = [[w for w in c if (len(w)>1)] for c in ip]

我想把我的 ip 以便我得到以下输出 op:

op= ['boy called me there', 'what doing over there ', "come me the end']
python python-3.x
1个回答
7
投票

当用c对ip进行迭代时,c变成了一个char(例如,'a','','b','o','y','',...)。

所以,试着把每句话用空格分割,然后计算长度。

示例代码在这里。

op = [' '.join([w for w in c.split(' ') if len(w) >= 2]) for c in ip]

1
投票

为了清晰起见,我会清理一下变量名,为了可读性,你可能会想把列表理解分解开来,但无论哪种方式都会像预期的那样发挥作用。

sentences = ['a boy called me z there', 'what u doing b over there ', "come w me t the end"]
processed = [' '.join(word for word in sentence.split(' ') if len(word) > 1) for sentence in sentences]

1
投票

尝试

for i in ip:
  p=i.split()
op=[]
for i in p:
  if(len(i)==1 ):
    op.append(i)
© www.soinside.com 2019 - 2024. All rights reserved.