2018年6月30日土曜日

開発環境

入門 自然言語処理 (Steven Bird (著)、Ewan Klein (著)、Edward Loper (著)、萩原 正人 (翻訳)、中山 敬広 (翻訳)、水野 貴明 (翻訳)、オライリージャパン)の2章(テキストコーパスと語彙資源へのアクセス)、2.8(演習問題)1.を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3

print('1.')
words1 = ['python', 'Python', 'PYTHON', 'The', 'the']
words2 = ['mac', 'windows', 'browser', 'safari', 'chrome', 'firefox', 'edge']

print(words1)
print(words2)

print(f'加算: {words1 + words2}')
print(f'乗算: {words1 * 2}')
print(f'添字: {words1[1]}')
try:
    print(words1[10])
except Exception as err:
    print(type(err), err)

print(f'スライス表記: {words1[:2]}\n{words1[2:]}\n{words1[::1]}')
print(f'{words1[::-1]}\n{words1[-2]}')

print('ソート')
words3 = sorted(words1)
print(words1)
print(words3)

words1.sort()
print(words1)

入出力結果(Terminal, Jupyter(IPython))

$ ./sample1.py
1.
['python', 'Python', 'PYTHON', 'The', 'the']
['mac', 'windows', 'browser', 'safari', 'chrome', 'firefox', 'edge']
加算: ['python', 'Python', 'PYTHON', 'The', 'the', 'mac', 'windows', 'browser', 'safari', 'chrome', 'firefox', 'edge']
乗算: ['python', 'Python', 'PYTHON', 'The', 'the', 'python', 'Python', 'PYTHON', 'The', 'the']
添字: Python
<class 'IndexError'> list index out of range
スライス表記: ['python', 'Python']
['PYTHON', 'The', 'the']
['python', 'Python', 'PYTHON', 'The', 'the']
['the', 'The', 'PYTHON', 'Python', 'python']
The
ソート
['python', 'Python', 'PYTHON', 'The', 'the']
['PYTHON', 'Python', 'The', 'python', 'the']
['PYTHON', 'Python', 'The', 'python', 'the']
$

0 コメント:

コメントを投稿