๐Ÿ”Ž Itertools ํŒŒํ—ค์น˜๊ธฐ

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ 0 : ์„ค๋ช…๋ง›์ง‘! ์ตœ๊ณ ๋‹ค!
์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ 1 : ๋” ๋งŽ์€ ํ•จ์ˆ˜, ๋” ์ž์„ธํ•œ ์˜ˆ์ œ
์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ 2 : ๊น”๋”, ๊ฐ„๊ฒฐํ•œ ๋‚ด์šฉ. ์ฒ˜์Œ ๋ณธ ๋ธ”๋กœ๊ทธ๋ผ ์˜ˆ์ œ๋ฅผ ๋งŽ์ด ๋”ฐ๋ผํ–ˆ์œผ๋‚˜, ์„ค๋ช…์ด ์•ˆ๋งž๋Š” ๋ถ€๋ถ„์ด ์กฐ๊ธˆ ์žˆ์Œ

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')]
profile
์˜ค์‚ฝ : ์˜ค๋Š˜๋„ ์‚ฝ์งˆ

0๊ฐœ์˜ ๋Œ“๊ธ€