프리렉 - FREELEC
http://freelec.co.kr/book/catalogue_view.asp?UID=134
이강성저
가) s[100]의 결과는 무엇인가?
나) s[1:100]의 결과는 무엇인가?
다) s[4:0]의 결과는 무엇인가?
s = 'spam'
s[100] # 인덱싱은 참조 범위를 넘어서면 IndexError가 발생한다.
s[1:100] # 슬라이싱이 참조 범위를 넘어서면 범위 내에서 처리한다.
s[4:0] # 시작값보다 끝값이 더 작은 경우에는 공문자열이 리턴된다.
# 가)
s = 'Somewhere on the rainbow'
s.replace('on', 'over')
# 나)
s = 'Sometimes, I feel like a motherless child.'
s.replace('.', '')
# 다)
# 정규식에 관해서는 26장 참조
s = '''Using Praat for Linguistic Research by Will Styler is a practical guidebook and information package designed to help you use the Praat phonetics software package more effectively in Phonetic or Phonological research. Although it was originally written in the Spring/Summer of 2011 for the 2011 Linguistic Institute's Praat workshop, it's now available for anybody who's interested.
The guidebook itself is a 60+ page compilation of walkthroughs, explanations, and tutorials explaining how to use Praat for a variety of measurements and tasks. Although the guide does start with "basic" tasks like opening sound files, measuring duration, formants, pitch, it also covers more "advanced" tasks like source-filter resynthesis, A1-P0 nasality measurement, formula manipulation of sounds and even Praat scripting.
Using Praat for Linguistic Research is completely free, and can be used for whatever purpose you see fit. However, the author does ask that any usage be accompanied with a link to this page, such that later users can download the most recent versions of the guide. The guide will be updated over time, both as Praat changes and as the author adds new information.
In addition to the guidebook, the author has also made available a collection of documents, presentations, scripts and sound files which, along with a short syllabus, formed the core of the LSA Praat workshop. The scripts are made freely available for use and code-cannibalism, and the sound files are free for use in teaching oneself to use Praat with the Guidebook and the syllabus/worksheet.
Using Praat for Linguistic Research is licensed under a Creative Commons Attribution ShareAlike (CC BY-SA) License. More information on this license is available here.
Any questions, comments, concerns or corrections should be sent to Will Styler, will (at) savethevowels (dot) org.'''
s.count('\n') # 공백 라인 포함
# 공백이 아닌 라인 수를 구하고 싶다면
len(list(filter(None, s.splitlines())))
s = '/usr/local/bin/python'
s.split('/')
s = '/usr/local/bin/python'
parts = s.split('/')
'/'.join(parts[:-1]), parts[-1]
import os
os.path.split(s)
s = 'spam ham'
ws = s.split()
ws.reverse()
' '.join(ws)
s = 'spam'
s[0] # 'spam'의 첫 번째 문자
s[0][0] # s[0] == 's' 이므로 연속으로 적용해도 첫 문자 's'가 나옴. 파이썬은 문자 자료형이 없고 모두 문자열 자료형임
s = 'spam Spam SpaM egg eGG Egg ham hAm'
s.lower().count('spam')
s = 'We propose programming in to start by making it possible to teach Python, an existing scripting language, and to focus on creating a new development environment and teaching materials for it.'
# 소문자로
s1 = s.lower()
# 알파벳을 제외한 모든 문자 제거
import re
s2 = re.sub('[^a-z]', ' ', s1)
s2
# 각 단어를 순서대로 출력
ws = s2.split()
ws.sort()
ws
import collections
collections.Counter(ws)
sorted(collections.Counter(ws).items())
# 직접 계산하는 방법
cnt = {}
for w in ws:
if w in cnt:
cnt[w] += 1
else:
cnt[w] = 1
sorted(cnt.items())
s = 'spam'
r = ''
i = len(s)
while i > 0:
i -= 1
r += s[i]
r
s = 'spam'
L = []
for c in s:
L.insert(0, c)
''.join(L)
# 더 쉽고 간단한 방법
s = 'spam'
s[::-1]
passwd = '''noriko:x:524:500:유화정:/home/noriko:/bin/bash
sky1004mu:x:525:500:김청:/home/sky1004mu:/bin/bash
hyeyroung:x:526:500:이혜령:/home/hyeyroung:/bin/bash
muu20:x:527:500:이현복:/home/muu20:/bin/bash'''
for line in passwd.splitlines():
fields = line.split(':')
if len(fields) >= 5:
print (fields[4])
t = ('a', 'b', 'c')
'|'.join(t)
s = """
<body bgcolor="#FFFFFF">
Click <a href="http://www.python.org/"> Here </a>
To connect to the most powerful tools in the world.
</body>
</html>
"""
import re
s1 = re.sub('<.*?>', '', s)
print(s1)
import urllib.request
s = urllib.request.urlopen('http://www.python.org/').read( )
print(s[:100]) # 일부만 출력해보자
type(s) # 웹에서 가져온 객체는 문자열이 아닌 바이트(bytes) 자료형임
type(s.decode()) # 이 것을 문자열(str)로 바꾸려면 decode() 메서드 적용
import re
s1 = re.sub('<.*?>', '', s.decode(), flags=re.DOTALL).strip()
print (s1)
# compose_hangul3.py
# 책 페이지 169
cho_list_eng = [ "r", "R", "s", "e", "E", "f", "a", "q", "Q", "t", "T", "d", "w", "W", "c", "z", "x", "v", "g"]
jung_list_eng = ["k", "o", "I", "O", "j", "p", "u", "P", "h", "hk", "hO", "hl", "y", "n", "nj", "np", "nl", "b", "m", "ml", "l"]
jong_list_eng = ["", "r", "R", "rt", "s", "sw", "sg", "e", "f", "fr", "fa", "fq", "ft", "fx", "fv", "fg", "a", "q", "qt", "t", "T", "d", "w", "C", "z", "x", "v", "g"]
def compose_hangul3(cho_e, jung_e, jong_e=''):
cho = cho_list_eng.index(cho_e)
jung = jung_list_eng.index(jung_e)
jong = jong_list_eng.index(jong_e)
code = 0xac00 + ((cho*21) + jung)*28 + jong
return chr(code)
print (compose_hangul3('g', 'k', 's'), compose_hangul3('r', 'm', 'f'))
def split_into_syllables(s):
jung_list_eng = 'k o I O j p u P h hk hO hl y n nj np nl b m ml l'.split()
s2 = s
pos = 0
syllable_start = []
phonemes = []
while s2:
n = 0
if s2[:2] in jung_list_eng:
n = 2
elif s2[0] in jung_list_eng:
n = 1
if n: # 모음
syllable_start.append(len(phonemes)-1)
phonemes.append(s2[:n])
jaso = 'v'
s2 = s2[n:]
pos += n
elif s2[0] in ' \t\n': # 공백
syllable_start.append(len(phonemes))
phonemes.append(s2[:1])
jaso = 'v'
s2 = s2[1:]
pos += 1
else: # 자음
phonemes.append(s2[:1])
jaso = 'c'
s2 = s2[1:]
pos += 1
syllable_start.append(len(phonemes))
return [tuple(phonemes[k1:k2]) for k1, k2 in zip(syllable_start[:-1], syllable_start[1:])]
s = 'tpwhdeodhkd gksrmf'
syllables = split_into_syllables(s)
for syll in syllables:
try:
print (compose_hangul3(*syll), end='')
except:
for c in syll[0]:
print(c, end='')