Pythonのリストからユニークな値を取得する

この記事では、Pythonリストから一意の値を取得するための3つの方法を理解します。大量の生データを扱う際に、生の入力データセットから一意で繰り返しのないデータセットを取り出す必要がある場面によく遭遇します。

以下に挙げた方法は、この問題を解決するのに役立ちます。早速始めましょう!


Pythonのリストから一意の値を取得する方法

Pythonのリストからユニークな値を取得するためには、次のいずれかの方法を使用できます。

  • Python set() method
  • Using Python list.append() method along with a for loop
  • Using Python numpy.unique() method

リストから重複のない値を取得するためにPythonのSet()を使用します。

前回のPythonセットのチュートリアルで見たように、セットは重複する値を1つだけ保持します。セットのこの特性を利用すると、Pythonのリストから一意の値を取得することができます。

  • Initially, we will need to convert the input list to set using the set() function.

文法(ぶんぽう):

set(input_list_name)
  • As the list gets converted to set, only a single copy of all the duplicate elements gets placed into it.
  • Then, we will have to convert the set back to the list using the below command/statement:

構文法:

list(set-name)
  • Finally, print the new list

例:
私はコーヒーが大好きです。

Translation:
私はコーヒーが大好きです。 (Watashi wa kōhī ga daisuki desu.)

list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

set_res = set(list_inp) 
print("The unique elements of the input list using set():\n") 
list_res = (list(set_res))
 
for item in list_res: 
    print(item) 

出力:

The unique elements of the input list using set():

25
75
100
20
12

2. Pythonのlist.append()とforループ

ユニークな要素を見つけるために、Pythonのforループとlist.append()関数を組み合わせて同じ結果を得ることができます。

  • At first, we create a new (empty) list i.e res_list.
  • After this, using a for loop we check for the presence of a particular element in the new list created (res_list). If the element is not present, it gets added to the new list using the append() method.

構文:
文法構造

list.append(value)
  • In case, we come across an element while traversing that already exists in the new list i.e. a duplicate element, in such case it is neglected by the for loop. We will use the if statement to check whether it’s a unique element or a duplicate one.

日本語で以下を言い換えてください。選択肢は1つだけです。

例文:
I am studying Japanese.
日本語を勉強しています。

list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

res_list = []

for item in list_inp: 
    if item not in res_list: 
        res_list.append(item) 

print("Unique elements of the list using append():\n")    
for item in res_list: 
    print(item) 
      

出力:

Unique elements of the list using append():

100
75
20
12
25

3. ユニークなアイテムを持つリストを作成するためのPythonのnumpy.unique()関数

PythonのNumPyモジュールには、numpy.uniqueという組み込み関数があり、numpy配列から一意のデータアイテムを取得することができます。

  • In order to get unique elements from a Python list, we will need to convert the list to NumPy array using the below command:

文法構造

numpy.array(list-name)
  • Next, we will use the numpy.unique() method to fetch the unique data items from the numpy array
  • and finally, we’ll print the resulting list.

The structure and rules of a language that determine how words and phrases can be combined to form sentences.

numpy.unique(numpy-array-name)

例えば:

import numpy as N
list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

res = N.array(list_inp) 
unique_res = N.unique(res) 
print("Unique elements of the list using numpy.unique():\n")
print(unique_res)
      

出力: 1つのオプションで以下を日本語で言い換えてください。

Unique elements of the list using numpy.unique():

[12  20  25  75 100]

結論

この記事では、Pythonリストのデータアイテムから一意の値を取得するさまざまな方法を紹介しました。


参考文献

  • NumPy function: numpy.unique() – SciPy documentation
コメントを残す 0

Your email address will not be published. Required fields are marked *