输入数据:
[{"is_sarcastic": 1, "headline": "thirtysomething scientists unveil doomsday clock of hair loss", "article_link": "https://www.theonion.com/thirtysomething-scientists-unveil-doomsday-clock-of-hai-1819586205"},
{"is_sarcastic": 0, "headline": "dem rep. totally nails why congress is falling short on gender, racial equality", "article_link": "https://www.huffingtonpost.com/entry/donna-edwards-inequality_us_57455f7fe4b055bb1170b207"}
]
预期产量:
["thirtysomething scientists unveil doomsday clock hair loss",
"dem rep totally nails why congress is falling short on gender racial equality"]
我能够通过后续的代码片段获得预期的输出。你知道吗
stop_words = ["a", "about", "above", "after", "again", "..."]
_corpus, _result = [], []
for text in data:
text_clean = [word for word in re.split('\W+', text['headline'])if word.lower() not in stop_words and len(word) > 2]
_corpus.append(' '.join(text_clean))
_result.append(text['is_sarcastic'])
我试图使它简洁,纯粹是为了学习的目的,但无法复制与下面列出的代码片段相同的结果。你知道吗
_corpus, _result = map(list, zip(
*[(''.join(word), text['is_sarcastic']) for text in data for word in re.split('\W+', text['headline'])
if word.lower() not in stop_words and len(word) > 2]))
我得到的是单词列表而不是字符串。例如:['thirtysomething', 'scientists', ...]
。你知道吗
我没有恰当地使用join
方法。我怎样才能做到这一点?你知道吗
编辑1:我的目标是得到字符串列表而不是单词列表。
编辑2:我没有包括整个数据集,因为我认为它与问题无关。
编辑3:请忽略这个帖子,我有一个很难沟通清楚。感谢你们的帮助。
编辑4:重新格式化问题。你知道吗
要将以下代码段转换为列表:
这不是一个好主意,因为代码已经不容易阅读了!您应该从函数开始:
如果需要列表理解,请使用单个列表来存储对:
这个循环很容易转换成一个列表。为了得到结果,
zip
需要重新创建两个元组的元素:或者,就像你所做的:
完整代码:
离你写的不远,但是
text['is_sarcastic']
放错地方了。你知道吗相关问题 更多 >
编程相关推荐