【Pythonの旅】第二編(二):リストとメタグループ
説明:
Pythonのリストは他の高度な言語の配列に似ていますが、Pythonのリストは操作が楽です.
Pythonのリストの学習主線は主にリストパラメータの操作をめぐって使用され、注目すべきは以下の通りである.
以下の内容は基本的に上の操作をめぐって説明します.
1.基本操作
・基本的な操作は主に以下の通りである.
・上に範囲がある場合、前後の2つの数字は、「虎頭蛇尾」、つまり前を取っても後を取らない
・次の環境で簡単なテストを行います.
(1)names[num]:数値numに並べ替えられた要素
(2)names[-1:]:最後の1要素
(3)names[-5:]:最後の5要素
(4)names[-5:-1]:最後の5つの要素を含むが、最後の1つの要素を含まない(すなわちnames[-1])
(5)names[:5]またはnames[0:5]:開始5要素(すなわちnamesを含まない[5])
(6)names[10:15]:names[10]からnames[14]
2.append()パラメータ
・基本機能:リストに要素を追加
・基本文法:
・append()パラメータはリストの一番後ろに要素を追加し、一度に1つしか追加できない.
・プレゼンテーションは以下の通り.
3.count()パラメータ
・基本機能:リスト内の同じ要素の個数を計算する
・基本文法:
・プレゼンテーションは以下の通り.
・ステップアッププレゼンテーション:count()文字列に含まれる文字列を比較できる
4.extend()パラメータ
・基本機能:別のリストの要素を現在のリストにまとめる
・基本文法:
・プレゼンテーションは以下の通り.
・append()はリストの要素を追加しています.
5.index()パラメータ
・基本機能:検索するリスト要素のインデックス番号を見つける
・基本文法:
・リストに同じ要素が複数ある場合、index()は1番目の同じ要素のインデックス番号のみを見つける
・プレゼンテーションは以下の通り.
6.inset()パラメータ
・基本機能:リスト内のある場所に要素を挿入する
・基本文法:
・プレゼンテーションは以下の通り.
--小さな統合操作:上のリストの「xpleaf」を「xpleaf_に置き換えるYip'
・Pythonの特性を十分に活用していない不器用な方法:
・目的は達成できるが、実際にはこの方法は不器用である.
・Python特性を活用する方法:
7.pop()パラメータ
・基本機能:リストの最後の要素を削除する
・基本文法:
・プレゼンテーションは以下の通り.
8.remove()パラメータ
・基本機能:リストで指定した内容の要素を削除する
・基本文法:
・プレゼンテーションは以下の通り.
・同じ要素が複数ある場合は、1つ目のみ削除します.
9.sort()パラメータ
・基本機能:リスト内の要素をASCII表の順に並べ替え、純数値要素が先になる
・基本文法:
・プレゼンテーションは以下の通り.
・複数の要素の文字列がある場合は、異なるものがあるまで文字列の中の文字順に比較する.
10.reverse()パラメータ
・基本機能:元のリストの要素を逆順に記憶する
・基本文法:
・プレゼンテーションは以下の通り.
11.リストに関連するその他の操作
--リストの要素を削除する:del
・remove()ではなく、インデックス番号を指定する必要があります.
・プレゼンテーションは以下の通り.
--文字列の内容をリストの要素に変更する:list()
・プレゼンテーションは以下の通り.
--リストの要素を文字列に接続します:''.join()
・str()は、リスト全体を文字列としてのみ使用します.
・リスト要素を文字列化する方法は次のとおりです.
・''.join()パラメータのリスト要素は文字列型のみであり、数値型ではありません.
12.タプルの言及
・タプルはリストと同じであるが、タプルの内容が生成されると修正できない.
・タプルは主に他人が修正できない内容を格納するために用いられる.
・タプルの関連操作のデモと説明は以下の通りである.
・タプルをリストにする場合はlist()関数を使用します.
Pythonのリストは他の高度な言語の配列に似ていますが、Pythonのリストは操作が楽です.
Pythonのリストの学習主線は主にリストパラメータの操作をめぐって使用され、注目すべきは以下の通りである.
names.append(
names.count(
names.extend(
names.index(
names.insert(
names.pop(
names.remove(
names.reverse(
names.sort(
以下の内容は基本的に上の操作をめぐって説明します.
1.基本操作
・基本的な操作は主に以下の通りである.
names[num]: num
names[-1:]: 1
names[-5:]: 5
names[-5:-1]: 5 , 1 ( names[-1])
names[:5]: 5 ( names[5])
names[0:5]: 5 ( names[5])
names[10:15]:names[10] names[14]
・上に範囲がある場合、前後の2つの数字は、「虎頭蛇尾」、つまり前を取っても後を取らない
・次の環境で簡単なテストを行います.
>>> names = ['xpleaf','yonghaoyip','CL']
>>> names.extend(range(30))
>>> names
['xpleaf', 'yonghaoyip', 'CL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
(1)names[num]:数値numに並べ替えられた要素
>>> names[2]
'CL'
(2)names[-1:]:最後の1要素
>>> names[-1:]
[29]
(3)names[-5:]:最後の5要素
>>> names[-5:]
[25, 26, 27, 28, 29]
(4)names[-5:-1]:最後の5つの要素を含むが、最後の1つの要素を含まない(すなわちnames[-1])
>>> names[-5:-1]
[25, 26, 27, 28]
(5)names[:5]またはnames[0:5]:開始5要素(すなわちnamesを含まない[5])
>>> names[:5]
['xpleaf', 'yonghaoyip', 'CL', 0, 1]
>>> names[0:5]
['xpleaf', 'yonghaoyip', 'CL', 0, 1]
(6)names[10:15]:names[10]からnames[14]
>>> names[10:15]
[7, 8, 9, 10, 11]
>>> names[10]
7
>>> names[14]
11
>>> names[15]
12
2.append()パラメータ
・基本機能:リストに要素を追加
・基本文法:
names.append('xpleaf')
・append()パラメータはリストの一番後ろに要素を追加し、一度に1つしか追加できない.
・プレゼンテーションは以下の通り.
>>> names.append('xpleaf')
>>> names
['xpleaf', 'yonghaoyip', 'CL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf']
3.count()パラメータ
・基本機能:リスト内の同じ要素の個数を計算する
・基本文法:
names.count('xpleaf')
・プレゼンテーションは以下の通り.
>>> names
['xpleaf', 'yonghaoyip', 'CL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf']
>>> names.count('xpleaf')
2
・ステップアッププレゼンテーション:count()文字列に含まれる文字列を比較できる
>>> name = 'xpleaf'
>>> name.count('xp')
1
>>> name.count('leaf')
1
>>> name.count('xpleaf123')
0
===> , 1, 0
4.extend()パラメータ
・基本機能:別のリストの要素を現在のリストにまとめる
・基本文法:
names.extend(range(10))
:
names = names + range(10) ===> ,Python
・プレゼンテーションは以下の通り.
>>> names
['xpleaf', 'yonghaoyip', 'CL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf']
>>> range(10) ===>range()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> names.extend(range(10))
>>> names
['xpleaf', 'yonghaoyip', 'CL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
・append()はリストの要素を追加しています.
names.append(range(10))
===> names [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5.index()パラメータ
・基本機能:検索するリスト要素のインデックス番号を見つける
・基本文法:
names.index('xpleaf')
・リストに同じ要素が複数ある場合、index()は1番目の同じ要素のインデックス番号のみを見つける
・プレゼンテーションは以下の通り.
>>> names.append('CL')
>>> names
['xpleaf', 'yonghaoyip', 'CL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'CL']
>>> names.index('xpleaf')
0
>>> names.index('CL')
2
>>> names[2]
'CL'
6.inset()パラメータ
・基本機能:リスト内のある場所に要素を挿入する
・基本文法:
names.insert(6,'love') ===> 6 'love'
・プレゼンテーションは以下の通り.
>>> names
['xpleaf', 'yonghaoyip', 'CL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'CL']
>>> names.insert(6,'love')
>>> names
['xpleaf', 'yonghaoyip', 'CL', 0, 1, 2, 'love', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'CL']
>>> names[6]
'love'
--小さな統合操作:上のリストの「xpleaf」を「xpleaf_に置き換えるYip'
・Pythonの特性を十分に活用していない不器用な方法:
>>> while True:
... if names.count('xpleaf') == 0:
... break
... else:
... names[names.index('xpleaf')] = 'xpleaf_Yip'
...
>>> names
['xpleaf_Yip', 'yonghaoyip', 'CL', 0, 1, 2, 'love', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'CL']
・目的は達成できるが、実際にはこの方法は不器用である.
・Python特性を活用する方法:
>>> for i in range(names.count('xpleaf')):
... names[names.index('xpleaf')] = 'xpleaf_Yip'
...
>>> names
['xpleaf_Yip', 'yonghaoyip', 'CL', 0, 1, 2, 'love', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'CL']
7.pop()パラメータ
・基本機能:リストの最後の要素を削除する
・基本文法:
names.pop()
・プレゼンテーションは以下の通り.
>>> names
['xpleaf_Yip', 'yonghaoyip', 'CL', 0, 1, 2, 'love', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'CL']
>>> names.pop()
'CL'
>>> names
['xpleaf_Yip', 'yonghaoyip', 'CL', 0, 1, 2, 'love', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8.remove()パラメータ
・基本機能:リストで指定した内容の要素を削除する
・基本文法:
names.remove('love') ===> 'love'
・プレゼンテーションは以下の通り.
>>> names
['xpleaf_Yip', 'yonghaoyip', 'CL', 0, 1, 2, 'love', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> names.remove('love')
>>> names
['xpleaf_Yip', 'yonghaoyip', 'CL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
・同じ要素が複数ある場合は、1つ目のみ削除します.
>>> names.remove(0)
>>> names
['xpleaf_Yip', 'yonghaoyip', 'CL', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'xpleaf_Yip', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9.sort()パラメータ
・基本機能:リスト内の要素をASCII表の順に並べ替え、純数値要素が先になる
・基本文法:
names.sort()
・プレゼンテーションは以下の通り.
>>> names.sort()
>>> names
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'CL', 'xpleaf_Yip', 'xpleaf_Yip', 'yonghaoyip']
・複数の要素の文字列がある場合は、異なるものがあるまで文字列の中の文字順に比較する.
10.reverse()パラメータ
・基本機能:元のリストの要素を逆順に記憶する
・基本文法:
names.reverse()
・プレゼンテーションは以下の通り.
>>> names.reverse()
>>> names
['yonghaoyip', 'xpleaf_Yip', 'xpleaf_Yip', 'CL', 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]
11.リストに関連するその他の操作
--リストの要素を削除する:del
・remove()ではなく、インデックス番号を指定する必要があります.
・プレゼンテーションは以下の通り.
>>> names
['yonghaoyip', 'xpleaf_Yip', 'xpleaf_Yip', 'CL', 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]
>>> names[2]
'xpleaf_Yip'
>>> del names[2]
>>> names
['yonghaoyip', 'xpleaf_Yip', 'CL', 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]
--文字列の内容をリストの要素に変更する:list()
・プレゼンテーションは以下の通り.
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> a = list(string.ascii_lowercase)
>>> a
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
--リストの要素を文字列に接続します:''.join()
・str()は、リスト全体を文字列としてのみ使用します.
>>> str(a)
"['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']"
・リスト要素を文字列化する方法は次のとおりです.
>>> ''.join(a)
'abcdefghijklmnopqrstuvwxyz'
>>> '_'.join(a)
'a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z'
・''.join()パラメータのリスト要素は文字列型のみであり、数値型ではありません.
>>> str(names)
"['yonghaoyip', 'xpleaf_Yip', 'CL', 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]"
>>> ''.join(names)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 3: expected string, int found
12.タプルの言及
・タプルはリストと同じであるが、タプルの内容が生成されると修正できない.
・タプルは主に他人が修正できない内容を格納するために用いられる.
・タプルの関連操作のデモと説明は以下の通りである.
>>> tuple = ('a', 'b', 'c', 1, 2, 3)
>>> tuple
('a', 'b', 'c', 1, 2, 3)
>>> tuple[3] ===> ,
1
>>> tuple[3] = 'b' ===>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> del tuple[3] ===>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
>>> list(tuple)
['a', 'b', 'c', 1, 2, 3]
>>> tuple. ===> tab count() index()
……
tuple.count(
tuple.index(
・タプルをリストにする場合はlist()関数を使用します.