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

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
4번째 줄: 4번째 줄:


==1차원 예시==
==1차원 예시==
<source lang='python' run>
<syntaxhighlight lang='python' run>
fruits = ['apple', 'banana', 'cherry']
fruits = ['apple', 'banana', 'cherry']


11번째 줄: 11번째 줄:
else:
else:
     print('미포함')
     print('미포함')
</source>
</syntaxhighlight>
<source lang='python' run>
<syntaxhighlight lang='python' run>
fruits = ['apple', 'banana', 'cherry']
fruits = ['apple', 'banana', 'cherry']


19번째 줄: 19번째 줄:
else:
else:
     print('포함')
     print('포함')
</source>
</syntaxhighlight>


==2차원 예시==
==2차원 예시==
<source lang='Python' run>
<syntaxhighlight lang='Python' run>
lsts = [['a', 'b'], ['c', 'a'], ['b', 'c']]
lsts = [['a', 'b'], ['c', 'a'], ['b', 'c']]


30번째 줄: 30번째 줄:
     else:
     else:
         print('미포함')
         print('미포함')
</source>
</syntaxhighlight>


==2차원 예시2==
==2차원 예시2==
<source lang='python' run>
<syntaxhighlight lang='python' run>
lsts = [['a', 'b'], ['c', 'd'], ['e', 'f']]
lsts = [['a', 'b'], ['c', 'd'], ['e', 'f']]


41번째 줄: 41번째 줄:
     else:
     else:
         print('미포함')
         print('미포함')
</source>
</syntaxhighlight>
:→ 리스트의 각 요소에서 첫번째 요소의 값만 확인
:→ 리스트의 각 요소에서 첫번째 요소의 값만 확인



2021년 7월 13일 (화) 10:00 판

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 }}