- 追加された行はこの色です。
- 削除された行はこの色です。
- Pythonメモ へ行く。
Pythonに関するメモ
#contents
*Python の list [#r729d5aa]
-list は [] 大カッコで宣言する
#geshi(python){{
>>> list = [1,2,3,4]
>>> list
[1, 2, 3, 4]
}}
-listの入れ子も柔軟に可能
#geshi(python){{
>>> list = [[1,2,3,4], [5,6],[7,8,9]]
>>> list
[[1, 2, 3, 4], [5, 6], [7, 8, 9]]
}}
-要素の指定も [] 大カッコ
#geshi(python){{
>>> list = [1,2,3,4,5,6,7]
>>> list
[1, 2, 3, 4, 5, 6, 7]
>>> list [4]
5
}}
--Pythonではlistは0-origin
-要素の追加はlistのappendという関数(?命令?)を使う
#geshi(python){{
>>> list = [1,2,3,4,5,6,7]
>>> list
[1, 2, 3, 4, 5, 6, 7]
>>> list.append(8)
>>> list
[1, 2, 3, 4, 5, 6, 7, 8]
}}
-要素の削除にはdelという関数(?命令?)を使う
#geshi(python){{
>>> list = [1,2,3,4,5,6,7,8]
>>> list
[1, 2, 3, 4, 5, 6, 7, 8]
>>> del list[3]
>>> list
[1, 2, 3, 5, 6, 7, 8]
}}
--appenedはlistの一部だが,delは独立した命令語
*pythonのリストをコピーする [#k04b4d82]
-pytonのリストの代入は参照コピー
#geshi(python){{
>>> a = [1,2,3]
>>> b = a
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> del a[1]
>>> a
[1, 3]
>>> b
[1, 3]
}}
-1次元リストを値コピーするためにはcopyを使う
#geshi(python){{
>>> import copy
>>> a = [1,2,3,4,5]
>>> b = copy.copy(a)
>>> a
[1, 2, 3, 4, 5]
>>> b
[1, 2, 3, 4, 5]
>>> del a[2]
>>> a
[1, 2, 4, 5]
>>> b
[1, 2, 3, 4, 5]
}}
-2次元リストを値コピーにするためにはdeepcopyを使う
#geshi(python){{
>>> import copy
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> b = copy.deepcopy(a)
>>> a
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> del a[2][2]
>>> del a[1]
>>> a
[[1, 2, 3], [7, 8]]
>>> b
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
}}
-[[8.17. copy ? Shallow and deep copy operations — Python v2.7.3 documentation>http://docs.python.org/2/library/copy.html]]¬e{copy-doc-python:[[8.17. copy ? Shallow and deep copy operations — Python v2.7.3 documentation>http://docs.python.org/2/library/copy.html]], Python v2.7.3準拠, 2013-03-27閲覧};
-[[3.18 copy -- 浅いコピーおよび深いコピー操作>http://docs.python.jp/2.4/lib/module-copy.html]]¬e{copy-deep-python:[[3.18 copy -- 浅いコピーおよび深いコピー操作>http://docs.python.jp/2.4/lib/module-copy.html]], 2006-06-27更新, 2013-03-27閲覧};
-[[Tsuchiya Yoshihiro: pythonメモ:リストのコピー>http://tsuchiya-yoshihiro.blogspot.jp/2008/03/python.html]]¬e{copy-python-yoshihiro:[[Tsuchiya Yoshihiro: pythonメモ:リストのコピー>http://tsuchiya-yoshihiro.blogspot.jp/2008/03/python.html]], 2008-03-13発表, 2010-09-29修正, 2013-03-27閲覧};
*Pythonでのfor文 [#gef85fb0]
#geshi(python){{
for i in range(10):
print i
}}
-実行結果
0
1
2
3
4
5
6
7
8
9
*printで改行なし出力 [#he2b6a4e]
#geshi(python){{
for i in range(10):
print i,
}}
-実行結果
0 1 2 3 4 5 6 7 8 9
[[【IT-rescue】 Python: print文で改行無しの出力>http://memo.jj-net.jp/324]]¬e{python-print-no-cr:[[【IT-rescue】 Python: print文で改行無しの出力>http://memo.jj-net.jp/324]], 2007-09-27発表, 2013-03-27閲覧};
*関数の定義方法 [#b7eda8ce]
#geshi(python){{
def check(x):
if 0 <= x < 5:
return 1
elif 5 <= x < 10:
return 2
else:
return False
}}
-[[お気楽 Python プログラミング入門:第2回関数とファイル入出力>http://www.geocities.jp/m_hiroi/light/python02.html]]¬e{file-io-python-hiroi:[[お気楽 Python プログラミング入門:第2回関数とファイル入出力>http://www.geocities.jp/m_hiroi/light/python02.html]], 2006-02-24発表, 2011-02-26修正, 2013-03-27閲覧};
-[[真(true)と偽(false) - 条件分岐 - Python入門>http://www.pythonweb.jp/tutorial/if/index3.html]]¬e{true-false-python:[[真(true)と偽(false) - 条件分岐 - Python入門>http://www.pythonweb.jp/tutorial/if/index3.html]], 2013-03-27閲覧};
*Pythonで日本語を扱う [#d9396c67]
#geshi(python){{
# coding:utf-8
}}
-を冒頭に書く
-[[pythonで「SyntaxError: Non-ASCII character」のエラーが出た場合の対処方法 - ドラあり!*ドラゴンに挑むアリの物語 ** Python使いの日々>http://d.hatena.ne.jp/omiyan/20110728]]¬e{japanese-python-omiyan:[[pythonで「SyntaxError: Non-ASCII character」のエラーが出た場合の対処方法 - ドラあり!*ドラゴンに挑むアリの物語 ** Python使いの日々>http://d.hatena.ne.jp/omiyan/20110728]], 2011-07-28発表, 2013-03-27閲覧};