2013-02-17 29 views
0

我试图从具有来自不同列表的#(哈希标签)的“句子”附加列表(空)。 目前我的代码给了我一个列表中涉及元素总数的新列表,而不是单个句子。用reg添加列表。表达式

的代码片段如下

import re 

old_list = ["I love #stackoverflow because #people are very #helpful!","But I dont #love hastags", 
"So #what can you do","Some simple senetnece","where there is no hastags","however #one can be good"] 

new_list = [ ] 


for tt in range(0,len(s)): 
    for ui in s: 
     if bool(re.search(r"#(\w+)",s[tt])) == True : 
      njio.append(s[tt]) 

给请让我知道如何追加仅单句。

回答

2

我不知道你想要什么样的输出,但这将保留原句与其匹配的集主题标签一起:

>>> import re 
>>> old_list = ["I love #stackoverflow because #people are very #helpful!","But I dont #love hastags", 
... "So #what can you do","Some simple senetnece","where there is no hastags","however #one can be good"] 
>>> hash_regex = re.compile('#(\w+)') 
>>> [(hash_regex.findall(l), l) for l in old_list] 
[(['stackoverflow', 'people', 'helpful'], 'I love #stackoverflow because #people are very #helpful!'), (['love'], 'But I dont #love hastags'), (['what'], 'So #what can you do'), ([], 'Some simple senetnece'), ([], 'where there is no hastags'), (['one'], 'however #one can be good')] 
+0

@sberry ......如何关闭这个问题。 – LonelySoul 2013-02-22 19:39:22

+0

只需点击左侧我答案旁边的复选标记。 – sberry 2013-02-24 02:17:07