2015-10-23 84 views
0

我想添加一列到已添加元素的列表中。在堆棧中看到類似的問題,但似乎沒有解決問題。將列添加到循環中的列表

下面是我正在做的事情: 有一個單詞列表和另一個系列的文本。我想選擇包含「單詞」列表中任何單詞的所有文本。已經獲得包含列表中任何單詞的文本,但我也想將各個單詞與特定文本關聯起來。 到目前爲止的代碼:

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

可以發佈期望的輸入和期望的輸出 – Hackaholic

+0

y沒有列表 – The6thSense

+0

@ Hackaholic,@ Vignesh Kalai:增加了一個例子。希望能幫助到你。 – eclairs

回答

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 
0

你可以定義一個FUNC,是以文本,並返回匹配:

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]