full.txt 包含:
www.example.com/a.jpg
www.example.com/b.jpg
www.example.com/k.jpg
www.example.com/n.jpg
www.example.com/x.jpg
partial.txt 包含:
a.jpg
k.jpg
为什么下面的代码没有提供想要的结果?
with open ('full.txt', 'r') as infile:
lines_full=[line for line in infile]
with open ('partial.txt', 'r') as infile:
lines_partial=[line for line in infile]
with open ('remaining.txt', 'w') as outfile:
for element in lines_full:
if element[16:21] not in lines_partial: #element[16:21] means like a.jpg
outfile.write (element)
所需的剩余.txt 应该具有 full.txt 中不在 partial.txt 中的那些元素,如下所示:
www.example.com/b.jpg
www.example.com/n.jpg
www.example.com/x.jpg