我有以下列表。
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']
当用c对ip进行迭代时,c变成了一个char(例如,'a','','b','o','y','',...)。
所以,试着把每句话用空格分割,然后计算长度。
示例代码在这里。
op = [' '.join([w for w in c.split(' ') if len(w) >= 2]) for c in ip]
为了清晰起见,我会清理一下变量名,为了可读性,你可能会想把列表理解分解开来,但无论哪种方式都会像预期的那样发挥作用。
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]
尝试
for i in ip:
p=i.split()
op=[]
for i in p:
if(len(i)==1 ):
op.append(i)