My code:
lis2 = []
lis1 = []
for cm in comments:
sp = cm.split()
for s in sp:
for tf in tfidf:
if tf == s:
lis2.append(tf)
else:
continue
lis1.append(lis2)
print(lis1)
data = pd.DataFrame(lis1)
In this code two lists:
comments
: list of sentencestfidf
: a list of words.I want to iterate every sentence (comments
) and find any word from tfidf
list and append it to a new list lis2
.
Also, when first sentence is finished, append lis2
to lis1
then go to the next sentence.
But my code just return word like this:
[['custom', 'servic', 'portfolio', 'time', 'custom', 'servic', 'custom', 'servic', 'support', 'ticket', 'custom', 'servic', 'experi', 'platform', 'user', 'experi', 'account', 'portfolio', 'experi', 'user', 'experi', 'user', 'platform', 'account', 'time', 'fast', 'platform', 'custom', 'custom', 'account', 'time', 'fast', 'time', 'time', 'account', 'custom', 'servic', 'servic', 'account', 'user', 'custom', 'custom', 'account', 'time', 'account', 'user', 'time', 'account']
comments = ['a1 a2 a3', 'b1 b2 b3', 'c1 c2 c3']
tfidf = ['a2', 'b1', 'b3']
lis_1 = []
for sentence in comments:
lis_2 = []
words = sentence.split()
for word in words:
if word in tfidf:
lis_2.append(word)
# After all words of a sentence are processed:
lis_1.append(lis_2)
print(lis_1)
Output:
[['a2'], ['b1', 'b3'], []]
You can get the same result more succinctly using a List Comprehension and sets:
lis_1 = [list(set(sentence.split()).intersection(tfidf)) for sentence in comments]