Python 程序,找出集合列表中的重复集合
需要在集合列表中查找重复集合时,需要使用“计数器”和“冻结集合”。
示例
以下是相同的演示:
from collections import Counter
my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, {1, 2, 6, 2}, {1, 4, 2}, {7, 8, 9}]
print("The list is :")
print(my_list)
my_freq = Counter(frozenset(sub) for sub in my_list)
my_result = []
for key, value in my_freq.items():
if value > 1 :
my_result.append(key)
print("The result is :")
print(my_result)输出
The list is :
[{8, 1, 4, 6}, {8, 1, 4, 6}, {1, 2, 6}, {1, 2, 4}, {8, 9, 7}]
The result is :
[frozenset({8, 1, 4, 6})]说明
定义了一个集合值列表并在控制台中显示。
使用“冻结集合”和“计数器”对其进行迭代。
这给出了列表中每个值出现的频率。
这被赋值给一个变量。
创建一个空列表。
对变量的元素进行迭代,如果频率大于 1,则将其附加到空列表中。
将其作为输出显示在控制台中。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP