2013年2月2日土曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)のまとめ演習2.(インデクシングとスライシング)を解いてみる。

その他参考書籍

2.(インデクシングとスライシング)

入出力結果(Terminal)

$ python
Python 3.3.0 (default, Sep 29 2012, 08:16:08) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> L = [0, 1, 2, 3]
>>> L[4] # エラー
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> L[-1000:100] # [0, 1, 2, 3]
[0, 1, 2, 3]
>>> L[3:1] # []
[]
>>> L[3:1] = ['?']
>>> L
[0, 1, 2, '?', 3]
>>> L[1:3] = [0,0,0]
>>> L
[0, 0, 0, 0, '?', 3]
>>> L[-1000:100] = [1,2]
>>> L
[1, 2]
>>> quit()
$

ちなみにJavaScriptの場合。

コード(BBEdit)

var result,
    a = [0, 1, 2, 3];
// undefined
result = a[4] + "\n";
// [0, 1, 2, 3]
result += a.slice(-1000, 1000) + "\n";
// []
result += a.slice(3, 1) + "\n";
a.splice(3, 0, ['?']);
// [0, 1, 2, '?', 3]
result += a + "\n";
a.splice(1, 2, [0,0,0]);
// [0, 0, 0, 0,, '?', 3]
result += a + "\n";
a.splice(-1000, 1099, [1,2]);
// [1, 2]
result += a + "\n";
$('#pre0').text(result);








						

0 コメント:

コメントを投稿