【发布时间】:2018-10-07 20:22:40
【问题描述】:
这里是 Python 菜鸟。 (全面披露)
我有一个被格式化为字符串列表的推文列表,如下所示:
["This is a string that needs processing #ugh #yikes",
"this string doesn't have hashtags",
"this is another one #hooray"]
我正在尝试编写一个函数,该函数将在每行中创建一个主题标签列表,但在没有任何条目时留下空白条目。这是因为我想稍后将推文本身加入这个列表。这是我想要的输出:
['#ugh', '#yikes'], [], ['#hooray']
我发现here 的这个函数适用于一个字符串。
mystring = "I love #stackoverflow because #people are very #helpful!"
但它似乎不适用于多个字符串。这是我的代码:
l = len(mystringlist)
it = iter(mystringlist)
taglist = []
def extract_tags(it,l):
for item in mystringlist:
output = list([re.sub(r"(\W+)$", "", j) for j in list([i for i in
item.split() if i.startswith("#")])])
taglist.append(output)
multioutput = extract_tags(mystringlist,l)
print(multioutput)
【问题讨论】:
标签: python arrays list pandas data-cleaning