Python Data Structures를 수강했다.
지난 Programming for Everybody의 심화과정인 느낌이다. 데이터를 다루는 함수들과 개념에 집중한 과정인 것 같았다. 난이도가 어렵진 않아서 쉽게 들을 수 있었다.
키워드
1주차 - 문자열(Strings), Slicing, in(논리연산자로써), String라이브러리(lower, find, upper, replace, lstrip, rstrip, strip, startswith)
2주차 - 과정의 원활한 진행을 위한 Python 관련 프로그램 설치
3주차 - 파일입출력(open, read, close), 줄바꿈문자, Skipping with Continue(조건적 반복문의 다른 형태)
4주차 - 배열, 배열의 Slicing과 Concatenating, 배열 관련함수(list, in, append, sort, sorted, len, max, min, sum, split), 범위지정함수(range)
5주차 - 딕셔너리, 딕셔너리 관련함수(dict, get, keys, values, items)
6주차 - 튜플, list comprehension
7주차 - 강의정리 및 마무리
과제 제출을 위해 작성한 코드는 아래와 같다.
문제포함한 코드 : https://github.com/colacan100/Python_Study
Assignment : 7.1
파일, 문자열관련함수
# Use words.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
line=line.upper()
line=line.rstrip()
print(line)
Assignment : 7.2
파일로부터 원하는 데이터추출(find이용)
# Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
s=0
c=0
su = float(s)
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue
num0 = line.find('0')
num = line[num0:]
fn = float(num)
su = su + fn
c = c+1
avg = su/c
print("Average spam confidence:",str(avg))
Assignment : 8.4
데이터 Slicing과 정렬
# Use the file name romeo.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
for i in line.rstrip().split():
lst.append(i)
lst = list(set(lst))
lst.sort()
print(lst)
Assignment : 8.5
파일로부터 원하는 데이터추출(split이용)
fname = input("Enter file name: ")
if len(fname) < 1:
fname = "mbox-short.txt"
fh = open(fname)
count = 0
for i in fh:
if 'From:' not in i:
continue
else:
a = i.split()
print(a[1])
count = count+1
print("There were", count, "lines in the file with From as the first word")
Assignment : 9.4
딕셔너리를 이용한 데이터추출
name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
histogram = dict()
for line in handle:
if not line.startswith('From:'):
continue
else:
sline = line.split()
email = sline[1]
histogram[email] = histogram.get(email,0) + 1
bigcount = None
bigemail = None
for mail,count in histogram.items():
if bigcount is None or count > bigcount:
bigemail = mail
bigcount = count
print(bigemail, bigcount)
Assignment : 10.2
딕셔너리와 튜플을 이용한 데이터 추출, 정렬
name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
count = dict()
for line in handle:
if not line.startswith("From "):
continue
else:
sline = line.split()
timeline = sline[5].split(':')
hour = timeline[0]
count[hour]=count.get(hour,0)+1
value=count.items()
nvalue = sorted(value)
for (v,k) in nvalue:
print(v,k)