简体   繁体   中英

Extracting all words starting with a certain character

I have a list of lists, in which I store sentences as strings. What I want to do is to get only the words starting with @. In order to do that, I split the sentences into words and now trying to pick only the words that start with @ and exclude all the other words.

# to create the empty list:
lst = []

# to iterate through the columns:
for i in range(0,len(df)):
    lst.append(df['col1'][i].split()) 

If I am mistaken you just need flat list containing all words starting with particular character. For doing that I would employ list flattening (via itertools ):

import itertools
first = 'f' #look for words starting with f letter
nested_list = [['This is first sentence'],['This is following sentence']]
flat_list = list(itertools.chain.from_iterable(nested_list))
nested_words = [i.split(' ') for i in flat_list]
words = list(itertools.chain.from_iterable(nested_words))
lst = [i for i in words if i[0]==first]
print(lst) #output: ['first', 'following']

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM