Python中的字典搜索

发布于 2025-01-22 19:52:16 字数 347 浏览 1 评论 0原文

我有一个包含数据和标题的数据集。我正在尝试通过所有头条新闻来应用词典搜索循环。

dict = pd.read_csv("suspectdict.csv")
news_df = pd.read_csv("news.csv")

suspectdict.csv中的单词包含描述动作的动词

被指控,被谋杀,谋杀,被捕,被捕..

。因此,在这种情况下,当它通过句子循环时,例如:

“一个被盗窃的当地男子”

因为被充电是在词典中,它将返回1 else 0 0

I have a dataset that contains data and headlines of articles. I am trying to apply dictionary search looping through all the headlines.

dict = pd.read_csv("suspectdict.csv")
news_df = pd.read_csv("news.csv")

words in suspectdict.csv contains verbs that describes the actions

charged, murdered, murder, caught, arrested..

and my news.csv consists of criminal articles. So in this case when it loops through the sentences and if for example:

"a local man charged for theft"

since charged is in the dictionary, it will return 1 else 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2025-01-29 19:52:17
dictionary = pd.read_csv("suspectdict.csv")
news_df = pd.read_csv("news.csv")

dict_set = set(dictionary)
news_df['suspected'] = newsdf['headline'].apply(lambda line: len(set(line.split()).intersect(dict_set)) > 0)

因此,您需要使您正在对集合进行检查的字典为O(1)。
然后,您可以通过检查集合交点的大小是否大于0,检查标题中的任何单词是否在字典中。

dictionary = pd.read_csv("suspectdict.csv")
news_df = pd.read_csv("news.csv")

dict_set = set(dictionary)
news_df['suspected'] = newsdf['headline'].apply(lambda line: len(set(line.split()).intersect(dict_set)) > 0)

So you'd want to make the dictionary you are checking against a set as inclusion in the set is O(1).
You could then check if any of the words of in the title are in the dictionary by checking for if the size of the set intersection is greater than 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文