๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป IT·๊ธฐ์ˆ ·ํ†ต๊ณ„

[Snippet] ์ƒํ™ฉ๋ณ„ Tuple, List ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•

by nowgeun 2023. 3. 10.
728x90

์ตœ๊ทผ์— NER ํƒœ๊น… ์ž‘์—…์„ ํ•˜๋ฉด์„œ tuple๊ณผ list๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์„ ๋งŽ์ด ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋˜์–ด ํฌ์ŠคํŒ…์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋งค๋ฒˆ ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ฐพ์•„๋‚ด์„œ ๋ณต๋ถ™ํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ๊ณผ์ • ์กฐ์ฐจ ๊ท€์ฐฎ์•„์ง€๊ฒŒ ๋˜์–ด์„œ ์ด๋ฒˆ ๊ธฐํšŒ์— ์•„์˜ˆ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

1. Tuple๋กœ ๊ตฌ์„ฑ๋œ List๋ฅผ ํ•˜๋‚˜์˜ List๋กœ ๋ณ€ํ™˜

ex. [('A',100), ('B',200), ('C',300), ('D',400), ('E',500)]  =>  ['A', 100, 'B', 200, 'C', 300, 'D', 400, 'E', 500]

 

# Tuple๋กœ ๋œ list
lst = [('A',100), ('B',200), ('C',300), ('D',400), ('E',500)]

# ๋ชจ๋“  element๋ฅผ ํ•˜๋‚˜์˜ list๋กœ ๋ณ€ํ™˜
## ๋ฐฉ๋ฒ• 1: List comprehension
new_lst1 = [e for tupl in lst for e in tupl]

## ๋ฐฉ๋ฒ• 2: for loop
new_lst2 = []
for x,y in lst:
    new_lst2.extend([x,y])
    
print(new_lst1)
print(new_lst2)
['A', 100, 'B', 200, 'C', 300, 'D', 400, 'E', 500]
['A', 100, 'B', 200, 'C', 300, 'D', 400, 'E', 500]

 

for๋ฌธ์„ ํ™œ์šฉํ•ด์„œ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ €๋Š” list comprehension ๋ฐฉ์‹์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ํ•œ์ค„๋งŒ์œผ๋กœ ์งค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ดํ•ดํ•  ๋•Œ๋Š” ๊ฐ€๋…์„ฑ์ด ์ƒ๋Œ€์ ์œผ๋กœ ๋–จ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ™ฉ์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

2. List์— ์žˆ๋Š” element๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ 2๊ฐœ ์”ฉ tuple๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ๋ณ€ํ™˜

1๋ฒˆ์˜ ๋ฐ˜๋Œ€ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค.

ex. ['A', 100, 'B', 200, 'C', 300, 'D', 400, 'E', 500] => [('A',100), ('B',200), ('C',300), ('D',400), ('E',500)]

 

# list
lst = ['A', 100, 'B', 200, 'C', 300, 'D', 400, 'E', 500]

# ๋ฐฉ๋ฒ•1: zip ์‚ฌ์šฉ
new_lst1 = list(zip(lst[0::2], lst[1::2]))

# ๋ฐฉ๋ฒ•2: for๋ฌธ๊ณผ list comprehension ์‚ฌ์šฉ
new_lst2 = [(lst[i],lst[i+1]) for i in range(0,len(lst),2)]

print(new_lst1)
print(new_lst2)
[('A', 100), ('B', 200), ('C', 300), ('D', 400), ('E', 500)]

 

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ €๋Š” zip๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• 1์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. (์ฝ”๋“œ๊ฐ€ ์งง์„ ์ˆ˜๋ก ๋ˆˆ์ด ๋œ ํ”ผ๋กœํ•œ ๊ฒƒ ๊ฐ™์•„์„œ ใ…Žใ…Ž...)

 

 

3. ๋‘ ๊ฐœ์˜ list์— ์žˆ๋Š” element ๊ฐ„์— ์ง์„ ์ง€์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜

ex.  ['A', 'B', 'C']   [1, 2]  =>  [('A', 1), ('A', 2), ('B', 1), ('B', 2), ('C', 1), ('C', 2)]

 

lst1 = ['A','B','C']
lst2 = [1,2]

# python built-in library itertools ์‚ฌ์šฉ
from itertools import product
new_lst1 = list(product(lst1, lst2))

# list comprehension ์‚ฌ์šฉ
new_lst2 = [(x,y) for x in lst1 for y in lst2]

## ์ฃผ์˜: lst1, lst2 ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง
new_lst3 = [(x, y) for x in lst2 for y in lst1]

# ์›ํ•˜๋Š” ๊ฐ’
print(new_lst1)
print(new_lst2)

# ํ‹€๋ฆฐ ๊ฐ’
print(new_lst3)
[('A', 1), ('A', 2), ('B', 1), ('B', 2), ('C', 1), ('C', 2)]
[('A', 1), ('A', 2), ('B', 1), ('B', 2), ('C', 1), ('C', 2)]
[(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'), (2, 'C')]

 

์•„์ฃผ ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์ด๊ธด ํ•˜์ง€๋งŒ... ์‚ฌ๋žŒ์€ ๋ง๊ฐ์˜ ๋™๋ฌผ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋” ์”ฉ ์ด๋ ‡๊ฒŒ ์ •๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ๊ธฐ์–ต๋ ฅ์— ๋„์›€์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€