Programming/SWEA

[SWEA 6275].[파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 2. 자료구조 -리스트, 튜플 2

토토모에요 2021. 7. 21. 12:52
728x90
반응형

SW Expert Academy에서 학습용으로 문제를 가져왔습니다. 문제가 될 시 수정, 삭제하겠습니다.

https://swexpertacademy.com/main/main.do

문제 : 리스트 내포 기능을 이용해 다음 문장으로부터 모음('aeiou')을 제거하십시오.
'Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open.'

input

output

Pythn s pwrfl... nd fst; plys wll wth thrs; rns vrywhr; s frndly & sy t lrn; s Opn.

code

a='Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open.'
b='aeiou'
a_comprehension=[char for char in a if char not in b]
print(''.join(a_comprehension))

파이썬에서는 for문과 if문을 한 라인에 작성하여 코드를 직관적으로 만들고 실행속도를 높혀주는 기법을 리스트 내포(List comprehension)라고 합니다. 쉽게 리스트명=[표현식 for 변수 in 반복 가능한 대상] 으로 생각하면 됩니다. 문자열이므로 char을 쓰고 만약 char가 b안에 있지 않을 경우만 뽑아서 새로운 리스트를 만듭니다. 그렇게 뽑은 리스트 a_comprehension은 각 문자가 따로 따로 떨어져있는 완성된 문장이 아니므로 join함수를 사용하여 서로 결합해 주어야됩니다.

반응형