야마타노오로치

일본어학과 학생들의 Python 도전기

모두를 위한 Python 특화과정/20171047 김동훈

python 데이터 구조-3

프로비우스윽 2022. 7. 27. 15:51
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
    line = line.rstrip()
    line=line.split()
    for kdh in line:
        if kdh in lst:
            continue
        else:
        	lst.append(kdh)
lst.sort()
print(lst)

결과

 

fname = input("Enter file name: ")
if len(fname) < 1:
    fname = "mbox-short.txt"

fh = open(fname)
count = 0
for line in fh:
    if not line.startswith("From:"):
        continue
    line= line.split()
    line=line[1]
    line = line.rstrip()
    count+=1
    print(line)

print("There were", count, "lines in the file with From as the first word")

결과