masterlist = ["subdomain.com", "subdomain2.com", "subdomain3.com", "subdomain4.com","subdomain5.com", "subdomain6.com"]
originaldomains = ["subdomain.com", "subdomain2.com", "subdomain32.com", "subdomain43.com","subdomain55.com", "subdomain6.com"]
将两个列表中的差异写入文件。
# Combine these into a masterlist
masterlist = list1 + list2
if os.path.exists('masterlist'):
print('Overwriting masterlist')
with open('masterlist', 'r') as f:
originalsubs = f.readlines()
我的主列表为var1,原始子目录为var2。
我想(希望)在我的if语句的同一代码块中写入差异吗?如果可能的话?
答案 0 :(得分:0)
使用Python Sets可以快速实现列表差异。值得知道这些是如何工作的。
masterlist = [ 'subdomain.com', 'subdomain2.com', 'subdomain3.com', 'subdomain4.com', 'subdomain5.com', 'subdomain6.com' ]
originaldomains = [ 'subdomain.com', 'subdomain2.com', 'subdomain33.com', 'subdomain44.com', 'subdomain5.com', 'subdomain6.com' ]
# get the differences
master_set = set( masterlist )
original_set = set( originaldomains )
difference_set = master_set.symmetric_difference( original_set )
# write to a file
fout = open( "diff.txt", "wt" )
for item in difference_set:
fout.write( item + "\n" )
fout.close()
我故意没有将其写成“完成的作业”标准。请阅读链接的文档,并用它来理解代码。然后编写您自己的解决方案。