0

我正在尝试使用“句子”附加一个列表(空),其中有来自不同列表的#(Hashtags)。目前,我的代码给了我一个新列表,其中包含列表中涉及的元素总数,而不是单个句子。

代码片段如下

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])

请让我知道如何只附加一个句子。

4

1 回答 1

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')]
于 2013-02-17T05:05:41.493 回答