PL(Programming Language)/Python

[Python] set ์ž๋ฃŒํ˜• ์ด์šฉํ•ด ๋ฆฌ์ŠคํŠธ ์ค‘๋ณต ์ œ๊ฑฐ + ๋ฆฌ์ŠคํŠธ ์ฐจ์ง‘ํ•ฉ

ํƒฑ์ ค 2021. 3. 25. 11:01

๋‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์„ ๋•Œ ์„œ๋กœ ์ค‘๋ณต๋˜๋Š” ๊ฐ’์„ ์ œ์™ธํ•œ ๊ฐ’๋“ค์„ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด set ์ž๋ฃŒํ˜•์„ ์ด์šฉ

a = [1, 2, 3, 4]
b = [2, 3, 5, 6, 7]

2, 3์ด ๊ฒน์น˜๋Š” ์›์†Œ

[x for x in a if x not in set(b)]โ€‹

์ˆœ์„œ๋ฅผ ๋ณด์กดํ•ด ์ฐจ์ง‘ํ•ฉ ์ถœ๋ ฅ

set(a) - set(b)

์ˆœ์„œ ๋ณด์กด x ์ฐจ์ง‘ํ•ฉ ์ถœ๋ ฅ


๋ฆฌ์ŠคํŠธ ์ฐจ์ง‘ํ•ฉ

a = ['abc', 'abcd', 'abcde']
b = ['bc', 'abc', 'abcd']
print([x for x in a if x not in b])
728x90