はじめに
この実験では、Python で集合(set)をリストに変換したときに、その結果がソートされたリストになるかどうかを判断する方法を学びます。この実験では、リストから重複を削除するための集合への変換を探索し、list() 関数を使用して集合をリストに戻す方法を示します。
次に、結果のリストがソートされているかどうかを、それ自身のソートされたバージョンと比較することで確認する方法を学びます。これには、リストを集合に変換し、結果の集合を出力し、集合をリストに戻し、最後にリストがソートされているかどうかを検証するメソッドを実装する Python スクリプトを作成することが含まれます。
集合(set)の変換を探索する
このステップでは、Python でリストを集合(set)に変換する方法を学びます。集合は、一意の要素からなる順序付けされていないコレクションです。つまり、集合には重複した値を含めることができません。リストを集合に変換することは、リストから重複する要素を削除する便利な方法です。
まず、VS Code エディタを使用して、~/project ディレクトリに convert_to_set.py という名前の Python ファイルを作成しましょう。
## Create a list with duplicate elements
my_list = [1, 2, 2, 3, 4, 4, 5]
## Convert the list to a set
my_set = set(my_list)
## Print the set
print(my_set)
次に、Python スクリプトを実行しましょう。ターミナルを開き、~/project ディレクトリに移動します。
cd ~/project
そして、python コマンドを使用してスクリプトを実行します。
python convert_to_set.py
以下の出力が表示されるはずです。
{1, 2, 3, 4, 5}
ご覧の通り、重複する要素が削除され、集合には一意の値のみが含まれています。
文字列の例も試してみましょう。
## Create a list of strings with duplicates
string_list = ["apple", "banana", "apple", "orange", "banana"]
## Convert the list to a set
string_set = set(string_list)
## Print the set
print(string_set)
convert_to_set.py に変更を保存し、再度スクリプトを実行します。
python convert_to_set.py
出力は次のようになります。
{'orange', 'banana', 'apple'}
集合は順序付けされていないため、集合内の要素の順序は元のリストと異なる場合があることに注意してください。
リストに変換してソートを確認する
このステップでは、集合(set)をリストに戻し、そのリストがソートされているかどうかを確認する方法を学びます。これには、list() 関数を使用して集合をリストに変換し、ソートされたリストと元のリストを比較することが含まれます。
~/project ディレクトリ内の convert_to_set.py ファイルを引き続き使用しましょう。集合をリストに戻し、そのリストをソートするコードを追加します。
## Create a list with duplicate elements
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
## Convert the list to a set to remove duplicates
my_set = set(my_list)
## Convert the set back to a list
my_list_from_set = list(my_set)
## Print the list obtained from the set
print("List from set:", my_list_from_set)
## Sort the list
my_list_from_set.sort()
## Print the sorted list
print("Sorted list:", my_list_from_set)
次に、Python スクリプトを実行しましょう。ターミナルを開き、~/project ディレクトリに移動します。
cd ~/project
そして、python コマンドを使用してスクリプトを実行します。
python convert_to_set.py
以下のような出力が表示されるはずです。
List from set: [1, 2, 3, 4, 5, 6, 9]
Sorted list: [1, 2, 3, 4, 5, 6, 9]
1 行目は集合から作成されたリストを示しており、このリストには一意の要素が含まれていますが、元の順序ではない場合があります。2 行目はソートされたリストを示しています。
次に、元のリスト(重複が削除され、リストに変換されたもの)がソートされているかどうかを確認するチェックを追加しましょう。
## Create a list with duplicate elements
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
## Convert the list to a set to remove duplicates
my_set = set(my_list)
## Convert the set back to a list
my_list_from_set = list(my_set)
## Sort the list
sorted_list = sorted(my_list_from_set)
## Check if the list is sorted
if my_list_from_set == sorted_list:
print("The list is already sorted.")
else:
print("The list is not sorted.")
## Print the sorted list
print("Sorted list:", sorted_list)
再度スクリプトを実行します。
python convert_to_set.py
以下のような出力が表示されるはずです。
The list is not sorted.
Sorted list: [1, 2, 3, 4, 5, 6, 9]
これは、集合から取得したリストが最初はソートされていなかったことを示しており、sorted() 関数を使用してソートされたバージョンが作成されたことを意味します。
sorted() を使用して比較する
このステップでは、元のリストを変更せずに sorted() 関数を使って比較する方法を学びます。sorted() 関数は、イテラブルオブジェクトから新しいソート済みリストを返します。一方、.sort() メソッドはリストをその場でソートします。元のリストをそのまま保ちたい場合にこの方法は便利です。
~/project ディレクトリ内の convert_to_set.py ファイルを引き続き使用しましょう。コードを変更して sorted() を使い、元のリストと比較するようにします。
## Create a list with duplicate elements
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
## Convert the list to a set to remove duplicates
my_set = set(my_list)
## Convert the set back to a list
my_list_from_set = list(my_set)
## Use sorted() to get a new sorted list
sorted_list = sorted(my_list_from_set)
## Print the original list from set
print("Original list from set:", my_list_from_set)
## Print the sorted list
print("Sorted list:", sorted_list)
## Check if the original list is equal to the sorted list
if my_list_from_set == sorted_list:
print("The original list is sorted.")
else:
print("The original list is not sorted.")
次に、Python スクリプトを実行しましょう。ターミナルを開き、~/project ディレクトリに移動します。
cd ~/project
そして、python コマンドを使ってスクリプトを実行します。
python convert_to_set.py
以下のような出力が表示されるはずです。
Original list from set: [1, 2, 3, 4, 5, 6, 9]
Sorted list: [1, 2, 3, 4, 5, 6, 9]
The original list is sorted.
この場合、集合から取得した元のリストはすでにソートされていました。元のリストが最初はソートされていないことを確認するために、元のリストを変更しましょう。
## Create a list with duplicate elements
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
## Convert the list to a set to remove duplicates
my_set = set(my_list)
## Convert the set back to a list
my_list_from_set = list(my_set)
## Shuffle the list to make sure it's not sorted
import random
random.shuffle(my_list_from_set)
## Use sorted() to get a new sorted list
sorted_list = sorted(my_list_from_set)
## Print the original list from set
print("Original list from set:", my_list_from_set)
## Print the sorted list
print("Sorted list:", sorted_list)
## Check if the original list is equal to the sorted list
if my_list_from_set == sorted_list:
print("The original list is sorted.")
else:
print("The original list is not sorted.")
再度スクリプトを実行します。
python convert_to_set.py
以下のような出力が表示されるはずです。
Original list from set: [9, 2, 4, 5, 6, 1, 3]
Sorted list: [1, 2, 3, 4, 5, 6, 9]
The original list is not sorted.
これで、元のリストはシャッフルされ、sorted() 関数が比較用のソート済みバージョンを提供しています。これにより、元のリストが変更されていないことがわかります。
まとめ
この実験(Lab)では、Python でリストを集合(set)に変換し、重複する要素を効果的に削除する方法を学びました。集合は順序付けされていないコレクションなので、変換中に要素の順序が変わる可能性があります。
また、集合をリストに戻し、得られたリストがソートされているかどうかを確認する方法の学習を始めました。これには list() 関数を使用し、ソートされたリストと元のリストを比較することが含まれます。



