我有蟒蛇两个列表list_a和list_b.在list_a有一些图片的链接,和list_b也.99%的项目是相同的,但我必须知道这1%.所有剩余物品都在list_a,这意味着所有物品list_b都在list_a.我最初的想法是减去所有项目:
list_a - list_b = list_c,这list_c是我的剩余项目.我的代码是:
list_a = []
list_b = []
list_c = []
arq_b = open('list_b.txt','r')
for b in arq_b:
list_b.append(b)
arq_a = open('list_a.txt','r')
for a in arq_a:
if a not in arq_b:
list_c.append(a)
arq_c = open('list_c.txt','w')
for c in list_c:
arq_c.write(c)
Run Code Online (Sandbox Code Playgroud)
我认为逻辑是正确的,如果我有一些项目,代码运行得很快.但我没有10项,或1.000,甚至100.000.78.514.022我list_b.txt和78.616.777我的列表中有项目list_a.txt.我不知道这个表达的代价:if a not in arq_b.但如果我执行此代码,我认为今年不会完成.
我的电脑有8GB,我分配15GB的交换,以免爆炸我的RAM.
我的问题是,还有另一种方法可以使这项操作更有效(更快)吗?
list_a …我有一个值列表和一个字典.我想确保列表中的每个值都作为字典中的键存在.目前我正在使用两套来确定字典中是否存在任何值
unmapped = set(foo) - set(bar.keys())
Run Code Online (Sandbox Code Playgroud)
有没有更多的pythonic方式来测试这个?感觉有点像黑客?
给定两个集合A和B及其长度:a = len(A)和b = len(B)其中a> = b。Python 2.7的issubset()函数(即B.issubset(A))的复杂性是什么?我可以从Internet找到两个矛盾的答案:
1,O(a)或O(b)
可以从以下网址找到:https : //wiki.python.org/moin/TimeComplexity和bit.ly/1AWB1QU
(很抱歉,我无法发布更多的http链接,因此必须改用Shortcut url。)
我从Python官方网站下载了源代码,发现:
def issubset(self, other):
"""Report whether another set contains this set."""
self._binary_sanity_check(other)
if len(self) > len(other): # Fast check for obvious cases
return False
for elt in ifilterfalse(other._data.__contains__, self):
return False
return True
Run Code Online (Sandbox Code Playgroud)
这里只有循环。
2,O(a * b)
从以下位置找到:bit.ly/1Ac7geK
我还发现一些代码看起来像来自以下代码的Python源代码:bit.ly/1CO9HXa如下:
def issubset(self, other):
for e in self.dict.keys():
if e not in other:
return False
return True
Run Code Online (Sandbox Code Playgroud)
这里有两个循环。
那么哪一个是对的?有人可以给我详细回答以上两种解释之间的区别吗?预先非常感谢。