提取所有以特定字符开头的单词

时间:2019-01-14 11:24:45

标签: python

我有一个列表列表,其中将句子存储为字符串。我想做的只是获取以@开头的单词。为此,我将句子分为单词,现在尝试仅选择以@开头的单词,并排除所有其他单词。

# to create the empty list:
lst = []

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

1 个答案:

答案 0 :(得分:0)

如果我弄错了,您只需要 flat 列表,其中包含以特定字符开头的所有单词。为此,我将使用列表展平(通过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']
相关问题