はじめに
この実験では、Python でセット (set) が特定のサイズを持っているかどうかを確認する方法を学びます。この実験では、様々なプログラミングタスクに不可欠な、セット内の要素数を判断するための len()
関数の使用に焦点を当てています。
Python スクリプトで数値と文字列のセットを作成し、len()
関数を使用してセットのサイズを求めます。最後に、セットのサイズを目的のサイズと比較して、一致するかどうかを確認します。
💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください
この実験では、Python でセット (set) が特定のサイズを持っているかどうかを確認する方法を学びます。この実験では、様々なプログラミングタスクに不可欠な、セット内の要素数を判断するための len()
関数の使用に焦点を当てています。
Python スクリプトで数値と文字列のセットを作成し、len()
関数を使用してセットのサイズを求めます。最後に、セットのサイズを目的のサイズと比較して、一致するかどうかを確認します。
このステップでは、len()
関数を使用してセット (set) 内の要素数を判断する方法を学びます。セットのサイズを理解することは、セットが空かどうかを確認したり、異なるセットのサイズを比較したりするなど、様々なプログラミングタスクにおいて重要です。
まず、Python スクリプトで簡単なセットを作成しましょう。LabEx 環境で VS Code エディタを開き、~/project
ディレクトリに set_size.py
という名前の新しいファイルを作成します。
## Create a set of numbers
my_set = {1, 2, 3, 4, 5}
## Print the set
print(my_set)
ファイルを保存します。次に、このスクリプトを実行して出力を確認しましょう。VS Code のターミナル (下部パネルにあります) を開き、~/project
ディレクトリに移動します (デフォルトでそこにいるはずです)。以下のコマンドを使用してスクリプトを実行します。
python set_size.py
以下の出力が表示されるはずです。
{1, 2, 3, 4, 5}
セットができたので、それが含む要素数を調べましょう。set_size.py
スクリプトに以下の行を追加します。
## Create a set of numbers
my_set = {1, 2, 3, 4, 5}
## Print the set
print(my_set)
## Get the size of the set using the len() function
set_size = len(my_set)
## Print the size of the set
print("The size of the set is:", set_size)
変更を保存し、再度スクリプトを実行します。
python set_size.py
今度は、以下の出力が表示されるはずです。
{1, 2, 3, 4, 5}
The size of the set is: 5
len()
関数はセット内の要素数を返します。この場合、セット my_set
は 5 つの要素を含んでいます。
文字列のセットを使った別の例を試してみましょう。set_size.py
スクリプトを以下のように変更します。
## Create a set of strings
my_set = {"apple", "banana", "cherry"}
## Print the set
print(my_set)
## Get the size of the set using the len() function
set_size = len(my_set)
## Print the size of the set
print("The size of the set is:", set_size)
ファイルを保存して実行します。
python set_size.py
以下の出力が表示されるはずです。
{'cherry', 'banana', 'apple'}
The size of the set is: 3
ご覧の通り、len()
関数はさまざまなデータ型を含むセットでも機能します。
前のステップでは、len()
関数を使ってセット (set) のサイズを取得する方法を学びました。このステップでは、セットと共に len()
関数を使用するより高度な方法を探ります。これには、条件文やループの中での使用が含まれます。
まず、set_size.py
スクリプトを変更して、セットが空かどうかをチェックする条件文を追加しましょう。VS Code エディタで set_size.py
ファイルを開き、以下のように変更します。
## Create a set of numbers
my_set = {1, 2, 3, 4, 5}
## Print the set
print(my_set)
## Get the size of the set using the len() function
set_size = len(my_set)
## Print the size of the set
print("The size of the set is:", set_size)
## Check if the set is empty
if set_size == 0:
print("The set is empty.")
else:
print("The set is not empty.")
ファイルを保存して実行します。
python set_size.py
以下の出力が表示されるはずです。
{1, 2, 3, 4, 5}
The size of the set is: 5
The set is not empty.
次に、スクリプトを変更して空のセットを作成し、そのサイズをチェックしましょう。set_size.py
スクリプトの最初の行を変更して空のセットを作成します。
## Create an empty set
my_set = set()
## Print the set
print(my_set)
## Get the size of the set using the len() function
set_size = len(my_set)
## Print the size of the set
print("The size of the set is:", set_size)
## Check if the set is empty
if set_size == 0:
print("The set is empty.")
else:
print("The set is not empty.")
ファイルを保存して再度実行します。
python set_size.py
今度は、以下の出力が表示されるはずです。
set()
The size of the set is: 0
The set is empty.
ご覧の通り、len()
関数は空のセットに対して 0 を返し、条件文はセットが空であることを正しく識別します。
次に、len()
関数をループの中で使用しましょう。セットが空になるまで要素を削除したいとします。set_size.py
スクリプトを以下のように変更します。
## Create a set of numbers
my_set = {1, 2, 3, 4, 5}
## Print the set
print(my_set)
## Remove elements from the set until it is empty
while len(my_set) > 0:
## Remove an arbitrary element from the set
element = my_set.pop()
print("Removed element:", element)
print("The set is now:", my_set)
print("The set is now empty.")
ファイルを保存して実行します。
python set_size.py
以下のような出力が表示されるはずです (削除される要素の順序は異なる場合があります)。
{1, 2, 3, 4, 5}
Removed element: 1
The set is now: {2, 3, 4, 5}
Removed element: 2
The set is now: {3, 4, 5}
Removed element: 3
The set is now: {4, 5}
Removed element: 4
The set is now: {5}
Removed element: 5
The set is now: set()
The set is now empty.
この例では、while
ループの各反復で len()
関数を使ってセットが空かどうかをチェックしています。pop()
メソッドはセットから任意の要素を削除します。セットが空になるまでループは続きます。
このステップでは、条件文を使用してセット (set) のサイズを目標サイズと比較する方法を学びます。特定の操作を実行する前に、セットが特定の要素数を含んでいることを確認する必要がある場合に、この方法は有用です。
set_size.py
スクリプトを変更して、セットのサイズを目標サイズと比較しましょう。VS Code エディタで set_size.py
ファイルを開き、以下のように変更します。
## Create a set of numbers
my_set = {1, 2, 3}
## Print the set
print(my_set)
## Get the size of the set using the len() function
set_size = len(my_set)
## Print the size of the set
print("The size of the set is:", set_size)
## Define the desired size
desired_size = 5
## Compare the size of the set with the desired size
if set_size == desired_size:
print("The set has the desired size.")
elif set_size < desired_size:
print("The set is smaller than the desired size.")
else:
print("The set is larger than the desired size.")
ファイルを保存して実行します。
python set_size.py
以下の出力が表示されるはずです。
{1, 2, 3}
The size of the set is: 3
The set is smaller than the desired size.
次に、スクリプトを変更して目標サイズのセットを作成しましょう。set_size.py
スクリプトの最初の行を変更して、5 つの要素を持つセットを作成します。
## Create a set of numbers
my_set = {1, 2, 3, 4, 5}
## Print the set
print(my_set)
## Get the size of the set using the len() function
set_size = len(my_set)
## Print the size of the set
print("The size of the set is:", set_size)
## Define the desired size
desired_size = 5
## Compare the size of the set with the desired size
if set_size == desired_size:
print("The set has the desired size.")
elif set_size < desired_size:
print("The set is smaller than the desired size.")
else:
print("The set is larger than the desired size.")
ファイルを保存して再度実行します。
python set_size.py
今度は、以下の出力が表示されるはずです。
{1, 2, 3, 4, 5}
The size of the set is: 5
The set has the desired size.
最後に、スクリプトを変更して目標サイズより大きいセットを作成しましょう。set_size.py
スクリプトの最初の行を変更して、7 つの要素を持つセットを作成します。
## Create a set of numbers
my_set = {1, 2, 3, 4, 5, 6, 7}
## Print the set
print(my_set)
## Get the size of the set using the len() function
set_size = len(my_set)
## Print the size of the set
print("The size of the set is:", set_size)
## Define the desired size
desired_size = 5
## Compare the size of the set with the desired size
if set_size == desired_size:
print("The set has the desired size.")
elif set_size < desired_size:
print("The set is smaller than the desired size.")
else:
print("The set is larger than the desired size.")
ファイルを保存して実行します。
python set_size.py
以下の出力が表示されるはずです。
{1, 2, 3, 4, 5, 6, 7}
The size of the set is: 7
The set is larger than the desired size.
これは、len()
関数を使用してセットのサイズを目標サイズと比較し、比較結果に基づいて異なるアクションを実行する方法を示しています。
この実験 (Lab) では、len()
関数を使用して Python のセット (set) のサイズを決定する方法を学びました。set_size.py
という名前の Python スクリプトを作成し、数値や文字列が含まれるセットを追加しました。その後、len()
関数を使用して各セット内の要素数を取得し、異なるデータ型でのその使用方法を示しました。
この実験では、セットを作成し、その内容を表示し、len()
を使用してセットのサイズを取得して表示する作業を行いました。このプロセスは、整数のセットと文字列のセットの両方で繰り返され、len()
関数を使ってセット内の要素数を効果的に決定する方法の理解を深めました。