"Python 리스트 요소 포함여부 확인 in"의 두 판 사이의 차이

16번째 줄: 16번째 줄:


if 'apple' not in fruits:
if 'apple' not in fruits:
    print('미포함')
else:
     print('포함')
     print('포함')
else:
    print('미포함')
</source>
</source>



2021년 7월 13일 (화) 09:47 판

1 개요

파이썬 리스트에 요소의 포함 여부 확인
  • 리스트에서 어떤 요소가 포함이 되어 있는지 확인 하는 방법

2 1차원 예시

fruits = ['apple', 'banana', 'cherry']

if 'apple' in fruits:
    print('포함')
else:
    print('미포함')
fruits = ['apple', 'banana', 'cherry']

if 'apple' not in fruits:
    print('미포함')
else:
    print('포함')

3 2차원 예시

lsts = [['a', 'b'], ['c', 'a'], ['b', 'c']]

for lst in lsts:
    if 'a' in lst:
        print('포함')
    else:
        print('미포함')

4 2차원 예시2

lsts = [['a', 'b'], ['c', 'd'], ['e', 'f']]

for lst in lsts:
    if 'a' in lst[0]:
        print('포함')
    else:
        print('미포함')
→ 리스트의 각 요소에서 첫번째 요소의 값만 확인

5 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}