python獲取列表惟一值

 

python 獲取惟一值python

 

In this article, we will be understanding 3 ways to get unique values from a Python list. While dealing with a huge amount of raw data, we often come across situations wherein we need to fetch out the unique and non-repeated set of data from the raw input data-set.數組

在本文中,咱們將瞭解從Python列表中獲取惟一值的3種方法 。 在處理大量原始數據時,咱們常常會遇到須要從原始輸入數據集中獲取惟一且未重複的數據集的狀況。app

The methods listed below will help you solve this problem. Let’s get right into it!ide

下面列出的方法將幫助您解決此問題。 讓咱們開始吧!函數

 
 


從Python列表中獲取惟一值的方法 (Ways to Get Unique Values from a List in Python)

Either of the following ways can be used to get unique values from a list in Python:oop

如下兩種方法都可用於從Python列表中獲取惟一值fetch

  • Python set() method

     

    Python set()方法
  • Using Python list.append() method along with a for loop

     

    使用Python list.append()方法和for循環
  • Using Python numpy.unique() method

     

    使用Python numpy.unique()方法


1. Python Set()從列表中獲取惟一值 (1. Python Set() to Get Unique Values from a List)

As seen in our previous tutorial on Python Set, we know that Set stores a single copy of the duplicate values into it. This property of set can be used to get unique values from a list in Python.ui

如咱們以前關於Python Set的教程中所見,咱們知道Set將重複值的單個副本存儲到其中。 set的此屬性可用於從Python列表中獲取惟一值。this

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

     

    最初,咱們須要使用set()函數將輸入列表轉換爲set。

Syntax:教程

句法:

  1.  
     
  2.  
    set(input_list_name)
  • As the list gets converted to set, only a single copy of all the duplicate elements gets placed into it.

     

    當列表轉換爲set時,全部重複元素的僅一個副本將放入其中。
  • Then, we will have to convert the set back to the list using the below command/statement:

     

    而後,咱們將必須使用如下命令/語句將集合轉換回列表:

Syntax:

句法:

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

     

    最後,打印新列表

Example:

例:

  1.  
     
  2.  
    list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
  3.  
     
  4.  
    set_res = set(list_inp)
  5.  
    print("The unique elements of the input list using set():\n")
  6.  
    list_res = (list(set_res))
  7.  
     
  8.  
    for item in list_res:
  9.  
    print(item)

Output:

輸出:

  1.  
     
  2.  
    The unique elements of the input list using set():
  3.  
     
  4.  
    25
  5.  
    75
  6.  
    100
  7.  
    20
  8.  
    12


2. Python list.append()和for循環 (2. Python list.append() and for loop)

In order to find the unique elements, we can apply a Python for loop along with list.append() function to achieve the same.

爲了找到惟一的元素,咱們能夠將Python for循環list.append()函數一塊兒使用以實現相同目的。

 
  • At first, we create a new (empty) list i.e res_list.

     

    首先,咱們建立一個新的(空)列表,即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.

     

    此後,使用for循環,咱們檢查所建立的新列表(res_list)中是否存在特定元素。 若是不存在該元素,則使用append()方法將其添加到新列表中。

Syntax:

句法:

  1.  
     
  2.  
    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.

     

    若是咱們在遍歷時遇到了一個在新列表中已經存在的元素,即重複元素,在這種狀況下,它被for循環所忽略。 咱們將使用if語句檢查它是惟一元素仍是重複元素。

Example:

例:

  1.  
     
  2.  
    list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
  3.  
     
  4.  
    res_list = []
  5.  
     
  6.  
    for item in list_inp:
  7.  
    if item not in res_list:
  8.  
    res_list.append(item)
  9.  
     
  10.  
    print("Unique elements of the list using append():\n")
  11.  
    for item in res_list:
  12.  
    print(item)
  13.  
     

Output:

輸出:

  1.  
     
  2.  
    Unique elements of the list using append():
  3.  
     
  4.  
    100
  5.  
    75
  6.  
    20
  7.  
    12
  8.  
    25

3. Python numpy.unique()函數建立包含惟一項的列表 (3. Python numpy.unique() function To Create a List with Unique Items)

Python NumPy module has a built-in function named, numpy.unique to fetch unique data items from a numpy array.

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:

     

    爲了從Python列表中獲取惟一元素,咱們須要使用如下命令將列表轉換爲NumPy數組:

Syntax:

句法:

  1.  
     
  2.  
    numpy.array(list-name)
  • Next, we will use the numpy.unique() method to fetch the unique data items from the numpy array

     

    接下來,咱們將使用numpy.unique()方法從numpy數組中獲取惟一數據項
  • and finally, we’ll print the resulting list.

     

    最後,咱們將打印結果列表。

Syntax:

句法:

  1.  
     
  2.  
    numpy.unique(numpy-array-name)

Example:

例:

  1.  
     
  2.  
    import numpy as N
  3.  
    list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
  4.  
     
  5.  
    res = N.array(list_inp)
  6.  
    unique_res = N.unique(res)
  7.  
    print("Unique elements of the list using numpy.unique():\n")
  8.  
    print(unique_res)
  9.  
     

Output:

輸出:

  1.  
     
  2.  
    Unique elements of the list using numpy.unique():
  3.  
     
  4.  
    [12 20 25 75 100]


結論 (Conclusion)

In this article, we have unveiled various ways to fetch the unique values from a set of data items in a Python list.

在本文中,咱們揭示了從Python列表中的一組數據項中獲取惟一值的各類方法。

相關文章
相關標籤/搜索