ソート
昇順にデータをソートする
sort関数を使用してソートする
list_sample = [2, 9, 3, 7, 4, 1, 0, 6, 8, 5] list_sample_sorted = list_sample.sort() print(list_sample) >>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(list_sample_sorted) >>None
ソートされた関数を出力するsorted関数も存在します.
list_sample = [2, 9, 3, 7, 4, 1, 0, 6, 8, 5] list_sample_sorted = sorted(list_sample) print(list_sample) >>[2, 9, 3, 7, 4, 1, 0, 6, 8, 5] print(list_sample_sorted) >>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
文字列もソート可能
list_sample = ['e', 'g', 'c', 'f', 'a', 'b'] list_sample.sort() print(list_sample) >>['a', 'b', 'c', 'e', 'f', 'g']
降順にデータをソートする
reverse = Trueにすることで降順ソートができます.
list_sample = [2, 9, 3, 7, 4, 1, 0, 6, 8, 5] list_sample_sorted = list_sample.sort(reverse = True) print(list_sample) >>[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] print(list_sample_sorted) >>None
sorted関数でも同様.
list_sample = [2, 9, 3, 7, 4, 1, 0, 6, 8, 5] list_sample_sorted = sorted(list_sample, reverse = True) print(list_sample_sorted) >>[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
多次元リストのソート
1番目の要素でソートする
多次元リストでもソート可能.内部リストの1番目の要素でソートされます
students = [ (3, 'A', 12), (2, 'B', 15), (1, 'B', 10),] students_sorted = sorted(students) print(students_sorted) >>[(1, 'B', 10), (2, 'B', 15), (3, 'A', 12)]
i番目の要素でソートする
内部リストの i 番目の要素でソートしたい場合.比較の際に使用される関数をkeyで指定できる
def func(student_tuple):
return student_tuple[2]
students = [ (3, 'A', 12), (2, 'B', 15), (1, 'B', 10),]
students_sorted = sorted(students, key=func)
print(students_sorted)
>>[(1, 'B', 10), (3, 'A', 12), (2, 'B', 15)]
labda関数を使えばよりシンプルに書ける.
students = [ (3, 'A', 12), (2, 'B', 15), (1, 'B', 10),] students_sorted = sorted(students, key=lambda student: student[2]) print(students_sorted) >>[(1, 'B', 10), (3, 'A', 12), (2, 'B', 15)]
降順にリストを並び替える
reverse関数を使用すれば降順に並び替えができます.
list_sample = [2, 9, 3, 7, 4, 1, 0, 6, 8, 5] list_sample.reverse() print(list_sample) >>[5, 8, 6, 0, 1, 4, 7, 3, 9, 2]
【応用】平均でソート
実装例として生徒の平均点でソートする場合を考えてみる
def func(student_score):
tmp = sum(student_score[1::])/float(len(student_score)-1)
return tmp
students = [['name:A', 37, 21, 58, 30, 47], ['name:B', 33, 39, 55, 86, 51], ['name:C', 9, 29, 77, 9, 71]]
students_sorted = sorted(students, key=func)
print(students_sorted)
>>[['name:A', 37, 21, 58, 30, 47], ['name:C', 9, 29, 77, 9, 71], ['name:B', 33, 39, 55, 86, 51]]
変換
タプルからリストへの変換
互いにキャストするだけで変換可能
tuple_sample = ('a', 'b', 'c')
list_sample = list(tuple_sample)
print(list_sample)
>>['a', 'b', 'c']
リストからタプルへの変換
逆も可能
list_sample = ['a', 'b', 'c']
tuple_sample = tuple(list_sample)
print(tuple_sample)
>>('a', 'b', 'c')
辞書型をリストに変換する
こちらも同様にキャストで実装可能です.指定しない場合はkeyがリストとなって出力されます.
dict_sample = {'a': 123, 'b':456, 'c':789}
list_sample = list(dict_sample)
print(list_sample)
>>['a', 'b', 'c']
valueから構成されるリストを取得したい場合はvalue関数を使用します.
dict_sample = {'a': 123, 'b':456, 'c':789}
list_sample = list(dict_sample.values())
print(list_sample)
>>[123, 456, 789]
valueから構成されるリストを取得したい場合はvalue関数を使用します.
dict_sample = {'a': 123, 'b':456, 'c':789}
list_sample = list(dict_sample.values())
print(list_sample)
>>[123, 456, 789]
文字列からリストに変換
文字列もインデックスでリストと同様に要素を獲得できます
str_sample = 'apple' print(str_sample[0]) >>a
キャストすることでリスト型に変換できます
str_sample = 'apple' list_sample= list(str_sample) print(list_sample) >>['a', 'p', 'p', 'l', 'e']
リストから文字列へ変換
for式を使い変換できます
list_sample = ['a', 'p', 'p', 'l', 'e'] for buf in list_sample: str_sample += buf print(str_sample) >>apple
0 件のコメント:
コメントを投稿