📜  计算Python列表中唯一值的数量

📅  最后修改于: 2022-05-13 01:55:35.074000             🧑  作者: Mango

计算Python列表中唯一值的数量

让我们看看如何计算Python列表中唯一值的数量。
例子 :

Input : 10 20 10 30 40 40
Output : 2
Explanation : Only 2 elements, 20 and 30 are unique in the list.

Input : 'geeks' 'for' 'geeks'
Output : 1

方法一:遍历列表,用字典统计每个元素的频率,最后统计频率为1的元素。

# function to count the unique elements
def count_unique(my_list):
  
    # variable to store the unique count
    count = 0
  
    # creating dictionary to count frequency
    freq = {}
  
    # traversing the list
    for x in my_list:
        if (x in freq):
            freq[x] += 1
        else:
            freq[x] = 1
  
    # traversing the dictionary
    for key, value in freq.items():
        if value == 1:
            count += 1
  
    # displaying the count of unique elements
    print(count)
  
# driver function
if __name__ == "__main__":
  
    my_list = [10, 20, 10, 30, 40, 40]
    count_unique(my_list)
  
    my_list = ['geeks', 'for', 'geeks']
    count_unique(my_list)

输出 :

2
1

时间复杂度: O(N)
空间复杂度: O(N)

方法 2:这里我们将使用字典类的get()方法来计算频率。这使程序更短,并演示了get()方法如何代替 if…else 有用。

# function to count the unique elements
def count_unique(my_list):
  
    # variable to store the unique count
    count = 0
  
    # creating dictionary to count frequency
    freq = {}
  
    # traversing the list
    for x in my_list:
        freq[x] = freq.get(x, 0) + 1
  
    # traversing the dictionary
    for key, value in freq.items():
        if value == 1:
            count += 1
  
    # displaying the count of unique elements
    print(count)
  
# driver function
if __name__ == "__main__":
  
    my_list = [10, 20, 10, 30, 40, 40]
    count_unique(my_list)
  
    my_list = ['geeks', 'for', 'geeks']
    count_unique(my_list)

输出 :

2
1

时间复杂度: O(N)
空间复杂度: O(N)

方法 3:这里我们将使用列表类的count()方法来计算频率。

# function to count the unique elements
def count_unique(my_list):
  
    # variable to store the unique count
    count = 0
  
    # creating dictionary to count frequency
    freq = {}
  
    # traversing the list
    for x in my_list:
        freq[x] = my_list.count(x)
  
    # traversing the dictionary
    for key, value in freq.items():
        if value == 1:
            count += 1
  
    # displaying the count of unique elements
    print(count)
  
# driver function
if __name__ == "__main__":
  
    my_list = [10, 20, 10, 30, 40, 40]
    count_unique(my_list)
  
    my_list = ['geeks', 'for', 'geeks']
    count_unique(my_list)

输出 :

2
1

时间复杂度: O(N)
空间复杂度: O(N)

方法 4:这里我们将使用集合模块的Counter()方法来计算频率。

# importing the module
import collections
  
# function to count the unique elements
def count_unique(my_list):
  
    # variable to store the unique count
    count = 0
  
    # creating dictionary to count frequency
    freq = collections.Counter(my_list)
  
    # traversing the dictionary
    for key, value in freq.items():
        if value == 1:
            count += 1
  
    # displaying the count of unique elements
    print(count)
  
# driver function
if __name__ == "__main__":
  
    my_list = [10, 20, 10, 30, 40, 40]
    count_unique(my_list)
  
    my_list = ['geeks', 'for', 'geeks']
    count_unique(my_list)

输出 :

2
1