Python列表规范化

2024-10-01 09:20:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个持续学习的网络爬虫,以查找与世界各地发生的特定危机和悲剧事件相关的新闻文章。我目前正在努力使数据模型尽可能精简和高效,因为随着爬网的继续,数据模型会不断增长

我将数据模型存储在一个列表中(用于与正在爬网的页面进行TFIDF比较)并且我希望减少列表的大小,但不丢失列表中每个项目的相对计数

这是来自2个已爬网网页的示例模型:

[[u'remark', u'special', u'agent', u'richard', u'deslauri', u'press', u'investig', u'crime', u'terror', u'crime', u'inform', u'servic', u'inform', u'laboratori', u'servic', u'want', u'want', u'want', u'terror', u'crime', u'want', u'news', u'news', u'press', u'news', u'servic', u'crime', u'inform', u'servic', u'laboratori', u'servic', u'servic', u'crime', u'crime', u'crime', u'terror', u'boston', u'press', u'remark', u'special', u'agent', u'richard', u'deslauri', u'press', u'investig', u'remark', u'special', u'agent', u'richard', u'deslauri', u'press', u'investig', u'boston', u'special', u'agent', u'remark', u'richard', u'deslauri', u'boston', u'investig', u'time', u'time', u'investig', u'boston', u'terror', u'law', u'enforc', u'boston', u'polic', u'polic', u'alreadi', u'alreadi', u'law', u'enforc', u'around', u'evid', u'boston', u'polic', u'evid', u'laboratori', u'evid', u'laboratori', u'may', u'alreadi', u'laboratori', u'investig', u'boston', u'polic', u'law', u'enforc', u'investig', u'around', u'alreadi', u'around', u'investig', u'law', u'enforc', u'evid', u'may', u'time', u'may', u'may', u'investig', u'may', u'around', u'time', u'investig', u'investig', u'boston', u'boston', u'news', u'press', u'boston', u'want', u'boston', u'want', u'news', u'servic', u'inform'], [u'2011', u'request', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'govern', u'crime', u'crime', u'inform', u'servic', u'inform', u'servic', u'nation', u'want', u'ten', u'want', u'want', u'crime', u'want', u'news', u'news', u'press', u'releas', u'news', u'stori', u'servic', u'crime', u'inform', u'servic', u'servic', u'servic', u'crime', u'crime', u'crime', u'news', u'press', u'press', u'releas', u'2011', u'request', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'govern', u'2011', u'request', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'govern', u'2013', u'nation', u'press', u'tamerlan', u'tsarnaev', u'dzhokhar', u'tsarnaev', u'tamerlan', u'tsarnaev', u'dzhokhar', u'tsarnaev', u'dzhokhar', u'tsarnaev', u'tamerlan', u'tsarnaev', u'dzhokhar', u'tsarnaev', u'2011', u'foreign', u'govern', u'inform', u'tamerlan', u'tsarnaev', u'inform', u'2011', u'govern', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'foreign', u'govern', u'2011', u'inform', u'foreign', u'govern', u'nation', u'press', u'releas', u'crime', u'releas', u'ten', u'news', u'stori', u'2013', u'ten', u'news', u'stori', u'2013', u'ten', u'news', u'stori', u'2013', u'2011', u'request', u'inform', u'tamerlan', u'tsarnaev', u'foreign', u'govern', u'nation', u'press', u'releas', u'want', u'news', u'servic', u'inform', u'govern']]

我希望维护单词列表,而不是将计数嵌入列表本身。我希望列表从以下位置开始:

[波士顿,波士顿,波士顿,爆炸,爆炸,查尔纳耶夫,查尔纳耶夫,时间]到[波士顿,波士顿,爆炸,查尔纳耶夫]

基本上,如果我有一个列表[a,a,a,b,b,c],我会把它减少到[a,a,b]

编辑:很抱歉不清楚,但我会再试一次。 我不想要一套。出现次数非常重要,因为它是一个加权列表,所以“Boston”应该比“time”或其他类似术语出现的次数更多。我试图做到的是最小化数据模型,同时从模型中删除不重要的项。所以在上面的例子中,我故意省略了C,因为它给模型增加了很多“脂肪”。我想保持相关性,因为A比B多出现1次,比C多出现2次,但由于C在原始模型中只出现一次,所以它将从精益模型中删除


Tags: 模型列表bostonnewspressforeignwantcrime
3条回答

如果将示例模型分配给变量topics,则可以使用collections.Counter来维护所有主题及其计数的类似字典的对象:

from collections import Counter
topic_count = [Counter(topic) for topic in topics]
# [Counter({u'boston': 11, u'investig': 11, u'crime': 7, u'servic': 7, u'want': 6, u'press': 6, u'laboratori': 5, u'may': 5, u'news': 5, u'agent': 4, u'alreadi': 4, u'deslauri': 4, u'special': 4, u'richard': 4, u'polic': 4, u'terror': 4, u'around': 4, u'evid': 4, u'law': 4, u'remark': 4, u'inform': 4, u'enforc': 4, u'time': 4}),
#  Counter({u'tsarnaev': 13, u'inform': 12, u'govern': 9, u'tamerlan': 9, u'foreign': 8, u'news': 8, u'crime': 8, u'2011': 7, u'servic': 7, u'press': 6, u'releas': 5, u'want': 5, u'ten': 4, u'request': 4, u'stori': 4, u'nation': 4, u'2013': 4, u'dzhokhar': 4})]
from collections import defaultdict
d = defaultdict(int)
for w in words[0]:
    d[w] += 1
mmin = min(d[p] for p in d)

然后你可以从每个单词中减去这个mmin,并创建一个新的列表。但也许这条格言足够紧凑。为了保持顺序,你可以使用dict中的信息,设计一些智能的方法来过滤你的初始单词列表

例如,对于单词列表[a,a,a,b,b,c],字典将包含{a:3, b:2, c:1}mmin=1。您可以使用此信息通过从所有项中减去1来获得{a:2, b:1},并且因为c0,所以它被删除

完整代码:

from collections import defaultdict
d = defaultdict(int)
words = ['a','a','a','b','b','c']
for w in words:
    d[w] += 1
mmin = min(d[p] for p in d)
slim=[]
for w in words:
    if d[w] > mmin:
        slim.append(w)
        d[w] -= 1
print slim

冒着过于简单化的风险,似乎leanmodel = model[::2]通常会做您想要的事情(在本例中,它正好做您想要的事情)

编辑:现在知道排序不重要了,我补充说:您应该首先对模型进行排序。这很容易在Python3或Python2.7中进行封装--leanmodel = sorted(model)[::2],而在Python2的早期版本中就不那么简单了

一般来说,这是一个1d最近邻采样问题(这就是为什么leanmodel = model[::2]可能足够精确。)

相关问题 更多 >