0. Counting Pattern
x = { 'a' :50, 'b' : 40, 'c': 60}
for aaa,bbb in x.items() : #item's'
print(aaa,bbb)
line = ' the the the i i i general pattetn to count the the words hehe'
words = line.split()
print(words)
counts = dict()
for word in words:
counts[word] = counts.get(word,0) +1
print(counts)
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
print(bigword, bigcount
1. tuple, sort, list comprehension
x = ('glenn', 'maggie', 'sally')
print(x)
x = (40, 30, 40, 10)
print(x)
print(max(x))
# ()의 경우, 구성성분을 변경할 수 없음.
# [] 구성성분은 변경 가능. ex) sort, append, reverse
# why do we use tuple? because it's more efficient.
(x,y) = (3, 'fred')
print(y) # == fred
(a,b) = (99,0)
print(a) # == 99
if ('jones', 'saa') < ('jones', 'sab') :
print('yes')
print( "\nsorted by key")
a= {'a':50, 'c':60, 'd':6, 'b':80}
a.items()
print(a.items())
print(sorted(a.items()))
for k,v in sorted(a.items()):
print(k,v)
print("\nsort by values")
x={'a':10, 'b':30, 'c':90}
y=list()
for k,v in x.items():
y.append((v,k)) #: k,v 위치를 바꾼 값을 y이름의 list에 추가함(append)
print(y)
print("\n 위의 식과 아래의 식은 같음 -> aka [list comprehension]")
print(sorted([(v,k) for k,v in x.items()]))
print("\n정렬")
n=sorted(y,reverse=True) #sorted(y, reverse=True) == y값을 내림차순 정렬
print(n)
m=sorted(y,reverse=False) #sorted(y, reverse=False) == y값을 오름차순 정렬
print(m)
- tuple, sort 연습
hand = open('ex_09_clown.txt')
di = dict()
for lin in hand:
lin = lin.rstrip()
wds = lin.split()
for w in wds :
di[w] = di.get(w,0) +1
print('\n', di)
x= di.items()
print('\n', x)
# 위의 값을 정렬하기
y = sorted(di.items())
print('\n', y)
print(y[:5])
a = list()
for k,v in di.items():
print(k,v)
newtup = (v,k)
a.append(newtup)
print("\nflipped->", a)
a= sorted(a)
print('\nsorted->', a[:5])
for v,k in a[:5]:
print(k,v)
'ONLINE COURSES > PYTHON' 카테고리의 다른 글
부스트코스 || 평균구하기 // split // Dictionary (0) | 2021.05.23 |
---|---|
부스트코스 || loop & iteration (0) | 2021.05.23 |
파이썬 입문 || class & object // package & module // is & else // try & except (0) | 2021.05.18 |
파이썬 입문 || for & while loop // break & continue // list // tuple & dictionary (0) | 2021.05.17 |
댓글