Python - 矩陣中的唯一值
Python 被世界各地不同的程式設計師用於不同的目的。Python 的不同應用領域包括 Web 開發、資料科學、機器學習,以及執行各種自動化流程。要繼續處理各種矩陣,瞭解矩陣中存在的不同值非常重要。在 Python 中表達矩陣的傳統方式是使用列表的列表,其中每個內部列表對應於矩陣的一行。矩陣的元素可以是任何資料型別,包括文字、浮點數,甚至整數。在本文中,我們將學習如何在矩陣中查詢唯一值。
查詢矩陣唯一值的不同方法
集合(Set)
這是一種查詢矩陣唯一值非常簡單的方法。我們可以簡單地將資料集轉換為集合,它會刪除所有公共元素,然後我們可以非常輕鬆地找到唯一值。讓我們舉個例子來更好地理解它
示例
def unique_values_of_matrix(data): # The data is given as input to the function unique_values_of_matrix
different_values = set() # An empty set is created
for row in data:
for element in row: # We check each element in the input given and then all the unique elements are added to the new set created (different_values)
different_values.add(element)
return different_values
# Example usage
Names = [
['John', 'Sam', 'Daniel'],
['Jack', 'Matt', 'Alaric'],
['Stefen', 'Damon', 'Tyler']
] # The input of the data is given
unique_values = unique_values_of_matrix(Names) # The function unique_values_of_matrix is run
print(unique_values) # The output will display all the unique values present in the matrix
輸出
以上示例的輸出如下所示
{'Stefen', 'Tyler', 'Matt', 'Alaric', 'Jack', 'Damon', 'John', 'Sam', 'Daniel'}}
NumPy
這是一個在 Python 中非常常用的庫,用於處理不同的數值。我們將使用 NumPy 庫中的一個函式來查詢矩陣的唯一值。該方法的示例如下所示
示例
import numpy as np # Do not forget to import numpy or else error might occur
# Example
Names = np.array([
['John', 'Sam', 'Daniel'],
['Jack', 'Matt', 'Alaric'],
['Stefen', 'Damon', 'Tyler']
]) # np.array defines the matrix data as array
different_values = np.unique(Names) # np.unique helps to find the unique values from the matrix
print(different_values)
輸出
以上示例的輸出如下所示
['Alaric' 'Damon' 'Daniel' 'Jack' 'John' 'Matt' 'Sam' 'Stefen' 'Tyler']
列表推導式
列表推導式用於有效地檢查列表中的每個元素。在這種方法中,我們將矩陣轉換為列表,然後找到其唯一值。讓我們舉個例子來更好地理解它
示例
# Example
Names = [
['John', 'Sam', 'Daniel'],
['Jack', 'Matt', 'Alaric'],
['Stefen', 'Damon', 'Tyler']
] # The input of the data is given
different_values = list({element for row in Names for element in row}) # With the help of list comprehension we check all the unique values in the matrix and then convert it back into a list
print(different_values) # The different unique values present in the list will be displayed
輸出
以上示例的輸出如下所示
['Alaric', 'Damon', 'Matt', 'John', 'Jack', 'Daniel', 'Stefen', 'Sam', 'Tyler']
Itertools
這是一個包含不同函式集的庫,用於有效地檢查列表中存在的不同元素。我們將使用 itertools 庫的函式來查詢矩陣中的唯一值。讓我們舉個例子來更好地理解它
示例
from itertools import chain # Do not forget to import itertools or else error might occur
# Example
Names = [
['John', 'Sam', 'Daniel'],
['Jack', 'Matt', 'Alaric'],
['Stefen', 'Damon', 'Tyler']
] # The input of the data is given
different_values = set(chain(*Names)) # The chain function of the itertool library is used to merge all the data from the matrix and flatten it and the set function is used to find the unique values of the matrix
print(different_values)
輸出
以上示例的輸出如下所示
{'Tyler', 'Jack', 'Stefen', 'Alaric', 'Sam', 'Damon', 'Daniel', 'Matt', 'John'}
Pandas 庫
當程式設計師必須處理矩陣中大量資料和許多不同矩陣時,很少使用這種方法。我們將使用 Pandas 庫的函式來查詢矩陣的唯一值。讓我們舉個例子來更好地理解它
示例
import pandas as pd # Do not forget to import the pandas library or else error might occur
# Example matrix
Names = [
['John', 'Sam', 'Daniel'],
['Jack', 'Matt', 'Alaric'],
['Stefen', 'Damon', 'Tyler']
] # The input of the data is given
new_dataframe = pd.DataFrame(Names) # A new data frame is created from the input
different_values = new_dataframe.stack().unique() # Stack function will help to stack the data into one place and unique() function will find the unique values
print(different_values) # The different unique values present in the list will be displayed
輸出
以上示例的輸出如下所示
['John' 'Sam' 'Daniel' 'Jack' 'Matt' 'Alaric' 'Stefen' 'Damon' 'Tyler']
結論
瞭解可以用來查詢矩陣唯一值的不同方法非常重要。本文介紹了查詢矩陣中存在的唯一值的不同方法。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP