์ฐธ๊ณ ๋ธ๋ก๊ทธ 0 : ์ค๋ช ๋ง์ง! ์ต๊ณ ๋ค!
์ฐธ๊ณ ๋ธ๋ก๊ทธ 1 : ๋ ๋ง์ ํจ์, ๋ ์์ธํ ์์
์ฐธ๊ณ ๋ธ๋ก๊ทธ 2 : ๊น๋, ๊ฐ๊ฒฐํ ๋ด์ฉ. ์ฒ์ ๋ณธ ๋ธ๋ก๊ทธ๋ผ ์์ ๋ฅผ ๋ง์ด ๋ฐ๋ผํ์ผ๋, ์ค๋ช ์ด ์๋ง๋ ๋ถ๋ถ์ด ์กฐ๊ธ ์์
accumulate(p,[func])
import itertools
accumulate = itertools.accumulate([x for x in range(1,10)])
result = []
for a in accumulate :
result.append(a)
print(result)
[1, 3, 6, 10, 15, 21, 28, 36, 45]
compress(d,s)
comp = itertools.compress('์๋
ํ์ธ์์ฐํฌ๋ก์น',[1,1,0,0,0,1,0,1,0])
comp # ๊ทธ๋ฅ ์ถ๋ ฅํ๋ฉด ์ ๋๋ ์ดํฐ ์ฝ๋๋ฅผ ์ถ๋ ฅํ๋ค
result = []
for a in comp:
result.append(a)
print(result)
['์', '๋
', '์ฐ', '๋ก']
# ๊ธ์ ๊ธธ์ด๊ฐ ์ ๋ง์ ๋? = ๋ค๋ 0์ผ๋ก ์ฒ๋ฆฌํ๊ณ ์ถ๋ ฅ ์ ํจ
comp = itertools.compress('์๋
ํ์ธ์์ฐํฌ๋ก์น',[1,0,1,0,1,0])
result = []
for a in comp:
result.append(a)
print(result)
['์', 'ํ', '์']
zip_longest(a,b,c)
zip = itertools.zip_longest('abce','ef',fillvalue='_')
zip
# ๊ทธ๋ฅ ์ถ๋ ฅํ๋ฉด ์ด๋ ๋ฏ ์๋์ด.
<itertools.zip_longest at 0x7f3c4ddb61d0>
for z in zip: print(z)
('a', 'e')
('b', 'f')
('c', '_')
('e', '_')
zip = itertools.zip_longest('abce','ef',fillvalue='_')
zip_result = []
for i in zip :
zip_result.append(i)
print(zip_result)
[('a', 'e'), ('b', 'f'), ('c', '_'), ('e', '_')]
# fillvalue ์์ ๋ = None๊ฐ
zip2 = itertools.zip_longest('abce','ef')
zip_result2 = []
for a in zip2 :
zip_result2.append(a)
print(zip_result2)
[('a', 'e'), ('b', 'f'), ('c', None), ('e', None)]
filterfalse(p,seq)
# p = ์กฐ๊ฑดff = itertools.filterfalse(lambda x : x < 5,[1,2,3,43,7,10,2,3,46,8,1])
ff_r = []
for f in ff:
ff_r.append(f)
print(ff_r)
[43, 7, 10, 46, 8]
takewhile(p,seq)
#p = ์กฐ๊ฑดtakewhile = itertools.takewhile(lambda x : x < 5, [1,2,3,4,5,6,5,4,3,1,2])
tw_r = []
for t in takewhile:
tw_r.append(t)
print(tw_r)
[1, 2, 3, 4]
dropwhile(p,seq)
dropwhile = itertools.dropwhile(lambda x : x < 5, [1,2,3,4,5,6,7,3,2,6,7,8])
dw_r = []
list(map(lambda x: dw_r.append(x), dropwhile))
print(dw_r)
[5, 6, 7, 3, 2, 6, 7, 8]
groupby(data, key=[None])
groupby = itertools.groupby('AAABBCCCCDDEEAFEEB')
for chr, group in groupby:
print(chr, ':', list(group),'id:',id(chr))
A : ['A', 'A', 'A'] id: 139897606416560
B : ['B', 'B'] id: 139897606548208
C : ['C', 'C', 'C', 'C'] id: 139897606548144
D : ['D', 'D'] id: 139897606548400
E : ['E', 'E'] id: 139897606154800
A : ['A'] id: 139897606416560
F : ['F'] id: 139897606548080
E : ['E', 'E'] id: 139897606154800
B : ['B'] id: 139897606548208
์๋ฅผ ๋ค์ด, ์์ ์์์ฒ๋ผ ์์์ A๋ฅผ ๋ถ๋ฅํด๋๊ณ , ๋ค์์ ๋ ๋ค์ A๋ฅผ ๊ฐ์ ธ์ค๋ฉด
๊ฐ์ A๋ผ๊ณ ์ธ์ํ์ง๋ง (id๊ฐ ๊ฐ์), ๋ฆฌ์คํธ๋ ์๋ก ๋ง๋ ๋ค.
๊ทธ๋์ ์ด๊ฑธ๋ก ์ ์ฒด ๋ฐ์ดํฐ ์์ ํด๋น ์์์ ์๋ฅผ ์๊ณ ์ ํ๋ค๋ฉด
๋ฆฌ์คํธ๋ก ๋ง๋ค์ด์ ๋ฆฌ์คํธ.sort()ํ ํ์ ์ฌ์ฉํด์ผ ํ๋ค.
์์ ์์ ์ธ 'AAABBCCCCDDEEAFEEB'์ฒ๋ผ
๋ถ์ด์๋ ๋ฌธ์์ด์ด๋ผ๋ฉด ๋ฌธ์์ด์ ๋ค ์์ ํ๋ํ๋๋ก ๋๋์ด ์ฃผ์ด์ผํ๋ค.
from itertools import groupby
test = 'AAABBCCCCDDEEAFEEB'
test = list(test)
test.sort()
print(test)
groupby = groupby(test)
for chr, group in groupby:
print(chr, ':', list(group),'id:',id(chr))
A : ['A', 'A', 'A', 'A'] id: 139897606416560
B : ['B', 'B', 'B'] id: 139897606548208
C : ['C', 'C', 'C', 'C'] id: 139897606548144
D : ['D', 'D'] id: 139897606548400
E : ['E', 'E', 'E', 'E'] id: 139897606154800
F : ['F'] id: 139897606548080
product(p,repeat = n)
- ์์ด : ์์ ์๊ด O
๊ทธ๋์ c c a์ c a c ๊ฐ ๋ค๋ฅธ ์์๋ก ์ทจ๊ธ๋์ด ๋์ค๋ ๊ฒ
prod = itertools.product('abc', repeat = 3)
print(list(a for a in prod))
# ๋ฆฌ์คํธ ์์ ์์๊ฐ ํํ์ธ ๊ฒ์ ๋ณด๋ฉฐ ํํ๋ก ์ถ๋ ฅํจ์ ํ์ธํ ์ ์๋ค.
[('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'b', 'a'), ('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'c', 'a'), ('a', 'c', 'b'), ('a', 'c', 'c'), ('b', 'a', 'a'), ('b', 'a', 'b'), ('b', 'a', 'c'), ('b', 'b', 'a'), ('b', 'b', 'b'), ('b', 'b', 'c'), ('b', 'c', 'a'), ('b', 'c', 'b'), ('b', 'c', 'c'), ('c', 'a', 'a'), ('c', 'a', 'b'), ('c', 'a', 'c'), ('c', 'b', 'a'), ('c', 'b', 'b'), ('c', 'b', 'c'), ('c', 'c', 'a'), ('c', 'c', 'b'), ('c', 'c', 'c')]
permutations(p,n)
perm = itertools.permutations('abcd',3)
print(list(a for a in perm))
[('a', 'b', 'c'), ('a', 'b', 'd'), ('a', 'c', 'b'), ('a', 'c', 'd'), ('a', 'd', 'b'), ('a', 'd', 'c'), ('b', 'a', 'c'), ('b', 'a', 'd'), ('b', 'c', 'a'), ('b', 'c', 'd'), ('b', 'd', 'a'), ('b', 'd', 'c'), ('c', 'a', 'b'), ('c', 'a', 'd'), ('c', 'b', 'a'), ('c', 'b', 'd'), ('c', 'd', 'a'), ('c', 'd', 'b'), ('d', 'a', 'b'), ('d', 'a', 'c'), ('d', 'b', 'a'), ('d', 'b', 'c'), ('d', 'c', 'a'), ('d', 'c', 'b')]
combination(p,n)
combination_with_replacement(p,n)
- ์กฐํฉ : ๋ง๊ทธ๋๋ก ์์ ์๊ดX ์กฐํฉ๋ง์ ๋ณธ๋ค.
์์๊ฐ ์ ๋ฆฌ๋ ๋ฆฌ์คํธ๋ฅผ p์ ๋ฃ์ผ๋ฉด ์์์๋ถํฐ ์กฐํฉ์ ๋ง๋ค๊ธฐ ๋๋ฌธ์ abcd๊ฐ ๋จผ์ ๋์ค๊ณ dacb ์ด๋ฐ์์ผ๋ก ์์ ์ฌ์ด์ ์์๊ฐ ๋ค์ฃฝ๋ฐ์ฃฝ ๋์ง ์๋ ๊ฒ ๊ฐ๋ค
comb = itertools.combinations('abcd',3)
print('comb',list(a for a in comb),sep = '\n')
comb_w_r = itertools.combinations_with_replacement('abcd',3)
print('comb_with_replacement',list(i for i in comb_w_r),sep='\n')
comb
[('a', 'b', 'c'), ('a', 'b', 'd'), ('a', 'c', 'd'), ('b', 'c', 'd')]
comb_with_replacement
[('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'a', 'd'), ('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'b', 'd'), ('a', 'c', 'c'), ('a', 'c', 'd'), ('a', 'd', 'd'), ('b', 'b', 'b'), ('b', 'b', 'c'), ('b', 'b', 'd'), ('b', 'c', 'c'), ('b', 'c', 'd'), ('b', 'd', 'd'), ('c', 'c', 'c'), ('c', 'c', 'd'), ('c', 'd', 'd'), ('d', 'd', 'd')]