Danish
Danish

Reputation: 2871

eliminate single character word in a list of sentence in pandas and numpy

I have a list of sentence as shown below

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

where each string which is separated by space is considered as word.

From the above list I would like to eliminate single character words and replace space with '-'.

Expected output:

x5 = ['abc-def-ghi' , 'ab-cd-ef-gh' ]

I tried below:

for item in list_x:
if (sum(len(x) == 1 for x in item.split(" "))) < 5:
x5.append(item.replace(' ','-'))

I would like to know is there any faster methods to do the above.

Upvotes: 1

Views: 26

Answers (1)

jezrael
jezrael

Reputation: 862691

One idea is use list comprehension here, if possible trailing spaces added 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']

Upvotes: 2

Related Questions