# itertools

ohjiaeยท2021๋ 12์ 26์ผ
0

๋ชฉ๋ก ๋ณด๊ธฐ
2/2

# ๐ Itertools ํํค์น๊ธฐ

## 1. accumulate

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]

## 2. compress

• compress(d,s)
• '1 or True'์ผ ๋๋ง ์ถ๋ ฅ
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)
['์', 'ํ', '์']

## 3. zip_longest

zip_longest(a,b,c)

• ์ธ๋ฑ์ค๊ฐ ๊ฐ์ ๊ฒ๋ผ๋ฆฌ ๋ง์ถฐ ํํ๋ก ์ถ๋ ฅํ๋ค.
• ๊ธธ์ด๊ฐ ๊ฐ์ฅ ๊ธด ๊ฒ์ด ๊ธฐ์ค์ด๋ฉฐ, ๋งค์นํ  ์์๊ฐ ๋ถ์กฑํ  ๋ fillvalue=''๋ก ๋์ฒด๊ฐ์ ์ง์ ํ  ์ ์๋ค.
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)]

## 4. filterfalse

• filterfalse(p,seq) # p = ์กฐ๊ฑด
• ์กฐ๊ฑด์ ์ด false ์ธ ๊ฒฝ์ฐ๋ง ์ถ๋ ฅ
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]

## 5. takewhile

• takewhile(p,seq) #p = ์กฐ๊ฑด
• seq ๊ฐ์ ํ์ํด์ false๊ฐ ๋๊ธฐ ์ ๊น์ง ์ํ (True๊ฐ ์ฐ์๋๋ ๊ฒฝ์ฐ๊น์ง๋ง)
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]

## 6. dropwhile

• dropwhile(p,seq)
• seq ๊ฐ์ ํ์ํด์ ์กฐ๊ฑด์ด false๊ฐ ๋๋ ์๊ฐ๋ถํฐ ๊ฐ์ ์ถ๋ ฅ
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]

## 7. groupby

• groupby(data, key=[None])
• SQL์ group by๋ ๋น์ทํด๋ณด์ด์ง๋ง, ๊ฐ์ key ๊ฐ์ ๊ฐ์ง๋ฉด ๊ฐ๋ค๊ณ  ๋ถ๋ฅํด๋ ์ถ๋ ฅ์ ์ฐ์๋๋ ๊ฒจ์ฐ์๋ง group์ผ๋ก ๋ฌถ๋๋ค.

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

## 8. product

• product(p,repeat = n)
• p๋ฅผ 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')]

## 9. permutations

• permutations(p,n)
• p๋ฅผ n๊ฐ์ ๊ธธ์ด๋ฅผ ๊ฐ์ง ์์ด์ ํํ๋ก ์ถ๋ ฅํ๋ค. (์์ ์ค๋ณต X)
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')]

## 10. combination, combination_with_replacement

• combination(p,n)
• combination_with_replacement(p,n)
• ์กฐํฉ์ ํํ๋ก ๋ฐํ. combination_with_replacement๋ ๋ฐ๋ณต ์์๊น์ง ํ์ฉํด์ ์ฐ์ฐ์ ์ํํ๋ค.
• ์กฐํฉ : ๋ง๊ทธ๋๋ก ์์ ์๊ด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')]
์ค์ฝ : ์ค๋๋ ์ฝ์ง