用於線在file2.splitlines(): 用於升在file1.splitlines(): 如果線路在升: 打印升for循環和嵌套for循環
0
A
回答
1
創建字典出在第二行的文件將是一個更快的解決方案,它將擺脫重複的太多:
from collections import defaultdict
second_file = open('second.txt')
second_file_dict = defaultdict(int)
first_file_dict = defaultdict(int)
for line in second_file:
second_file_dict[line.strip()] += 1
second_file.close()
first_file = open('first.txt')
for line in first_file:
if line in second_file_dict and not in first_file_dict:
print line
first_file_dict[line.strip()] += 1
first_file.close()
1
print list(set(file2.splitlines()) & set(file1.splitlines()))
+0
'splitlines()'返回一個列表,'&'操作符不用於名單。你的意思是把這些轉換成集合? –
+0
@ F.J:是的,我當時做過,但我忘了趕時間:)這就是爲什麼我把整個表達式轉換成'list',如果我沒有想到集合,這將是沒有意義的。 – bosnjak
相關問題
- 1. 嵌套for循環
- 2. 嵌套'for'循環
- 3. 嵌套for循環
- 4. 嵌套for循環跳躍循環
- 5. VBA循環遍歷嵌套for循環
- 6. for循環嵌套在while循環中
- 7. While循環嵌套for循環
- 8. JSP嵌套For循環
- 9. 嵌套for循環在R
- 10. Python |嵌套for循環
- 11. Openmp - 嵌套for循環
- 12. 通用嵌套for循環
- 13. 嵌套for循環問題
- 14. 重構嵌套for循環
- 15. 嵌套for循環藝術
- 16. PHP嵌套for-each循環
- 17. Windows shell嵌套for循環
- 18. For循環嵌套函數
- 19. 關於嵌套for循環
- 20. 嵌套的for循環
- 21. 修復嵌套for循環
- 22. 多個嵌套for循環
- 23. 嵌套for循環困難
- 24. 許多嵌套for循環
- 25. Python:慢嵌套for循環
- 26. Processing.js嵌套for循環
- 27. JavaScript的嵌套For循環
- 28. 嵌套for ...循環在SAS
- 29. 替代嵌套for循環
- 30. 重構嵌套for循環
什麼是你到底想幹什麼?打印每個文件的行? – dursk
請舉例:你得到了什麼?你想要什麼?謝謝 – sshashank124
你能用英文寫出你想要的輸出是什麼嗎? – dursk