我正在尝试将列添加到已添加元素的列表中。在堆栈上看到过类似的问题,但似乎都没有解决问题。
以下是我要做的事情: 有一个单词列表和另一系列文本。我想选择包含“单词”列表中任何单词的所有文本。已经获得包含列表中任何单词的文本,但我还想将相应单词与特定文本相关联。 代码到目前为止:
com=[]
for t in text.c:
for w in words:
if w in t:
com.append(t)
com = com + [w]
目前,它在文本下方的不同列中添加了第w个单词。如何添加列以便将相应的单词添加到不同的列但是同一行? com是一个列表。
Eg:
text:
c
0 this is good
1 You can improve more photos
2 development is required
3 keep up the good word
4 add more pics from different angles
5 add more good photos
6 this is not good for you
words=['good','photos']
required output:
0 this is good good
1 You can improve more photos photos
2 keep up the good word good
3 add more good photos good
4 add more good photos photos
5 this is not good for you good
答案 0 :(得分:0)
您可以定义一个获取文本并返回匹配项的函数:
In [126]:
words=['good','photos']
def func(x):
found=[]
for word in words:
if word in x:
found.append(word)
if len(found) > 0:
return found
df['found'] = df['c'].apply(func)
df
Out[126]:
c found
index
0 this is good [good]
1 You can improve more photos [photos]
2 development is required None
3 keep up the good word [good]
4 add more pics from different angles None
5 add more good photos [good, photos]
6 this is not good for you [good]
答案 1 :(得分:0)
我通过使用函数:namedtuple()
来达到上面的输出该功能可用作:
Lang = namedtuple("Lang", ("word", "c"))
com=[]
for f in c:
for w in words:
if w in f:
com.append(Lang(w,f))
output:
0 this is good good
1 You can improve more photos photos
2 keep up the good word good
3 add more good photos good
4 add more good photos photos
5 this is not good for you good