๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ชฉ๋กํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ ๋นผ๊ธฐ (1)

Hey Tech

[Python] ๋ฆฌ์ŠคํŠธ ๊ฐ„ ๋บ„์…ˆ(์ฐจ์ง‘ํ•ฉ) ๊ตฌํ˜„๋ฐฉ๋ฒ•

ํŒŒ์ด์ฌ์—์„œ 2๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ ๊ฐ„์— ๊ฒน์น˜๋Š” ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ฆ‰ ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด 2๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. list1 = ['a', 'b', 'c', 'd'] list2 = ['b', 'd'] โŒ ์ž˜๋ชป๋œ ๋ฐฉ๋ฒ• ๋ฆฌ์ŠคํŠธ๋ผ๋ฆฌ ํ•ฉ์ง‘ํ•ฉ์ธ ์›์†Œ ํ•ฉ์น˜๊ธฐ๋Š” ๋ง์…ˆ(+)์œผ๋กœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ฐจ์ง‘ํ•ฉ์„ ์œ„ํ•œ ๋บผ์…ˆ์€ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. list1-list2 ๊ฒฐ๊ณผ โœ… ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ• ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜(List Comprehension)์œผ๋กœ ์‰ฝ๊ฒŒ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. sub_set = [x for x in list1 if x not in list2] ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…๋‹ˆ๋‹ค. sub_set ๊ฒฐ๊ณผ ํฌ์ŠคํŒ… ๋‚ด์šฉ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์˜ค๋Š˜๋„..

SW ๊ฐœ๋ฐœ/Python 2022. 7. 4. 12:33