トップ 履歴 一覧 Farm ソース 検索 ヘルプ PDF RSS ログイン

common.py

*disclaimer
603414

 Python 3用

common3.py(251)


# copyleft 2011-05-08 sugiura@nagoya-u.jp
# 二つのファイルに共通する単語を選び出す。
# 前提:二つのファイルには、一行一単語で並んでいること

#Python 3用に修正
# copyleft 2018-03-27 sugiura@nagoya-u.jp


import time

f1 = open(input("Put in the first file name with double-quote marks: "))
firstFile = f1.readlines()
linesA = len(firstFile)
print(linesA, "items are included.\n")


f2 = open(input("Put in the second file name with double-quote marks: "))
secondFile = f2.readlines()
linesB = len(secondFile)
print(linesB, "items are included.\n")

f3 = open('common.txt', 'w')

for x in range(linesA):
        are = firstFile[x]
        for y in range(linesB):
                kore = secondFile[y]
                if are == kore:
                        f3.write(are)

f3 = open('common.txt', 'r')
thirdFile = f3.readlines()
linesC = len(thirdFile)
print("=============================")
print(linesC, "items are common.")
print("=============================")
time.sleep(2)

for z in range(linesC):
        print("(",z+1,")", thirdFile[z])

print("=============================\n")
print("That's it! Open the file called common.txt.")

time.sleep(3)

 Python 2用

common.py(659) (for Python 2)


# copyleft 2011-05-08 sugiura@nagoya-u.jp
# 二つのファイルに共通する単語を選び出す。
# 前提:二つのファイルには、一行一単語で並んでいること

import time

f1 = open(input("Put in the first file name with double-quote marks: "))
firstFile = f1.readlines()
linesA = len(firstFile)
print linesA, "items are included.\n"


f2 = open(input("Put in the second file name with double-quote marks: "))
secondFile = f2.readlines()
linesB = len(secondFile)
print linesB, "items are included.\n"

f3 = open('common.txt', 'w')

for x in range(linesA):
        are = firstFile[x]
        for y in range(linesB):
                kore = secondFile[y]
                if are == kore:
                        f3.write(are)

f3 = open('common.txt', 'r')
thirdFile = f3.readlines()
linesC = len(thirdFile)
print "============================="
print linesC, "items are common."
print "============================="
time.sleep(2)

for z in range(linesC):
        print "(",z+1,")", thirdFile[z]

print "=============================\n"
print "That's it! Open the file called common.txt."

time.sleep(3)