[문제] 주어진 문자열에 대하여 단어 단위로 분리된 문자열 리스트를 만들어라. 그리고 이 리스트에 저장된 단어들을 알파벳 순으로 정렬하고, 각 단어를 순서대로 출력하라.
주어진 문자열은 다음과 같습니다.
s = '''We propose to start by making it possible to teach programming in Python, an existing scripting
language, and to focus on creating a new development environment and teaching materials for it.'''
language, and to focus on creating a new development environment and teaching materials for it.'''
먼저 이 문자열을 단어 단위로 분리합니다.
L = s.split()
그리고 반환된 리스트의 함수의 하나인 sort() 를 이용해서 알파벳 순으로 정렬합니다.
L.sort()
결과를 한번 볼까요.
print L
[결과]
['Python,', 'We', 'a', 'an', 'and', 'and', 'by', 'creating', 'development', 'environment', 'existing', 'focus', 'for', 'in', 'it', 'it.', 'language,', 'making', 'materials', 'new', 'on', 'possible', 'programming', 'propose', 'scripting', 'start', 'teach', 'teaching', 'to', 'to', 'to']
[결과]
['Python,', 'We', 'a', 'an', 'and', 'and', 'by', 'creating', 'development', 'environment', 'existing', 'focus', 'for', 'in', 'it', 'it.', 'language,', 'making', 'materials', 'new', 'on', 'possible', 'programming', 'propose', 'scripting', 'start', 'teach', 'teaching', 'to', 'to', 'to']
정렬이 되긴 했는데 대문자로 시작하는 단어가 먼저 나와 버렸죠. 대소문자 구별없이 정렬하는 방법은 여러가지가
있겠지만, 저는 함수를 직접 정의하는 방법을 사용해서 해보겠습니다.
def MyCmp(a, b):
return (a.lower(), b.lower())
return (a.lower(), b.lower())
위 함수를 L.sort() 의 앞에 위치시키고, sort() 함수를 다음과 같이 수정합니다.
L.sort(MyCmp)
제대로 정렬이 되는지 다시 출력해볼까요.
print L
[결과]
['a', 'an', 'and', 'and', 'by', 'creating', 'development', 'environment', 'existing', 'focus', 'for', 'in', 'it', 'it.', 'language,', 'making', 'materials', 'new', 'on', 'possible', 'programming', 'propose', 'Python,', 'scripting', 'start', 'teach', 'teaching', 'to', 'to', 'to', 'We']
[결과]
['a', 'an', 'and', 'and', 'by', 'creating', 'development', 'environment', 'existing', 'focus', 'for', 'in', 'it', 'it.', 'language,', 'making', 'materials', 'new', 'on', 'possible', 'programming', 'propose', 'Python,', 'scripting', 'start', 'teach', 'teaching', 'to', 'to', 'to', 'We']
대소문자 구별없이 알파벳 순으로 단어가 정렬된 것이 보이시죠.
그럼 이제 각 단어를 순서대로 출력해보도록 하겠습니다.
for el in L:
print el
print el
[출력 결과]
a
an
and
and
by
creating
development
environment
existing
focus
for
in
it
it.
language,
making
materials
new
on
possible
programming
propose
Python,
scripting
start
teach
teaching
to
to
to
We
a
an
and
and
by
creating
development
environment
existing
focus
for
in
it
it.
language,
making
materials
new
on
possible
programming
propose
Python,
scripting
start
teach
teaching
to
to
to
We
간단하죠^^
아래는 지금까지 설명한 내용에 대한 전체 코드입니다.
# 110 페이지 연습문제 8번
s = '''We propose to start by making it possible to teach programming in Python, an existing scripting language, and to focus on creating a new development environment and teaching materials for it.'''
L = s.split()
def MyCmp(a, b):
return cmp(a.lower(), b.lower())
s = '''We propose to start by making it possible to teach programming in Python, an existing scripting language, and to focus on creating a new development environment and teaching materials for it.'''
L = s.split()
def MyCmp(a, b):
return cmp(a.lower(), b.lower())
L.sort(MyCmp)
for el in L:
print el
Python, 문자열 정렬, 열혈강의 연습문제, 열혈강의 Python, 열혈강의 파이썬, 파이

