I have the following list:
ip= ['a boy called me z there', 'what u doing b over there ', "come w me t the end']
I want to remove all the single letters from each of these strings within the list.
I have tried the following but it does not work:
x = [[w for w in c if (len(w)>1)] for c in ip]
I want to convert my ip
such that I get the following output op
:
op= ['boy called me there', 'what doing over there ', "come me the end']
When iterating ip by c, c becomes a char (eg, 'a', ' ', 'b', 'o', 'y', ' ', ...)
So, try split each sentence by space and count the length.
Example code is here.
op = [' '.join([w for w in c.split(' ') if len(w) >= 2]) for c in ip]
try
for i in ip:
p=i.split()
op=[]
for i in p:
if(len(i)==1 ):
op.append(i)
I'd clean up the variable names for clarity, and you might want to break out the list comprehensions for readability, but either will function as expected.
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.