我有如下清单。你知道吗
mylist = [[5274919, ["report", "porcelain", "firing", "technic"]], [5274920, ["implantology", "dentistry"]], [52749, ["method", "recognition", "long", "standing", "root", "perforation", "molar"]], [5274923, ["exogenic", "endogenic", "cause", "tooth", "jaw", "anomaly", "method", "method", "standing"]]]
我还有一个概念列表,如下所示。你知道吗
myconcepts = ["method", "standing"]
我想看看myconcepts
中的每个概念在mylist
记录中出现了多少次。i、 e
"method" = 2 times in records (i.e. in `52749` and `5274923`)
"standing" = 2 times in records
我现在的代码如下。你知道吗
mycounting = 0
for concept in myconcepts:
for item in mylist:
if concept in item[1]:
mycounting = mycounting + 1
print(mycounting)
但是,我现在的mylist
非常大,有大约500万条记录。myconcepts
这个列表有大约10000个概念。你知道吗
在我当前的代码中,一个概念需要将近1分钟才能得到count
,这非常慢。你知道吗
我想知道在python中最有效的方法是什么?你知道吗
出于测试目的,我将数据集的一小部分附加在:https://drive.google.com/file/d/1z6FsBtLyDZClod9hK8nK4syivZToa7ps/view?usp=sharing
如果需要,我很乐意提供更多细节。你知道吗
将概念列表更改为集合,这样搜索将是O(1)。然后可以使用交集来计算每个集合中的匹配数。你知道吗
如果要分别获取每个概念的计数,则需要在
myconcept
上循环,然后使用in
运算符。你可以把结果放进字典里。你知道吗这仍然比使用列表更有效,因为
concept in l[1]
是O(1)。你知道吗从https://www.geeksforgeeks.org/python-count-the-sublists-containing-given-element-in-a-list/调整方法3
*修改当前方法(只计算一次计数)*
性能(使用Jupyter笔记本比较发布的方法)
结果表明,该方法与Barmar贴纸法相近(即36对42 us)
对当前方法的改进减少了大约一半的时间(即从36 us减少到19 us)。对于更多的概念(即问题有超过1000个概念),这种改进应该更为重要。你知道吗
然而,原来的方法速度更快,为2.55us/圈。你知道吗
方法当前方法
方法2(来自Barmar post)
方法3(原始方法)
您可以展平输入,然后使用
collections.Counter
:输出:
编辑:记录查找:
输出:
相关问题 更多 >
编程相关推荐