您的位置:

Python集合:高效存储和处理数据的利器

一、集合的基本定义和操作

Python的集合是一种无序的,不重复的数据类型,可以进行交集、联合、差集等操作。集合在数据分析和清洗的过程中起到了至关重要的作用。

创建一个集合可以使用花括号{}或set()函数。

{1, 2, 3, "a", "b", "c"}
set([1,2,3,"a","b","c"])

可以使用len()函数获取集合的长度,使用in关键字判断元素是否在集合中,使用add()函数添加元素。

s = {1,2,3,"a","b","c"}
len(s) # 返回6
"a" in s # 表达式的值为True
s.add("d") # 添加元素"d"

二、集合的操作

集合提供了一些常见的操作,包括联合、交集、差集和对称差集。

联合操作:使用union()函数或者|运算符可以实现两个集合的联合,返回一个包含两个集合中所有不重复元素的新集合。

s1 = {1,2,3,"a","b","c"}
s2 = {3,4,5,"c","d","e"}
s3 = s1.union(s2) # 返回 {1,2,3,4,5,"a","b","c","d","e"}
s4 = s1 | s2 # 返回 {1,2,3,4,5,"a","b","c","d","e"}

交集操作:使用intersection()函数或者&运算符可以实现两个集合的交集,返回一个包含两个集合中共同元素的新集合。

s1 = {1,2,3,"a","b","c"}
s2 = {3,4,5,"c","d","e"}
s3 = s1.intersection(s2) # 返回 {3,"c"}
s4 = s1 & s2 # 返回 {3,"c"}

差集操作:使用difference()函数或者-运算符可以实现两个集合的差集,返回一个包含一个集合中不包含于另一个集合的元素的新集合。

s1 = {1,2,3,"a","b","c"}
s2 = {3,4,5,"c","d","e"}
s3 = s1.difference(s2) # 返回 {1,2,"a","b"}
s4 = s1 - s2 # 返回 {1,2,"a","b"}

对称差集操作:使用symmetric_difference()函数或者^运算符可以实现两个集合的对称差集,返回一个包含两个集合中不重复元素的新集合。

s1 = {1,2,3,"a","b","c"}
s2 = {3,4,5,"c","d","e"}
s3 = s1.symmetric_difference(s2) # 返回 {1,2,4,5,"a","b","d","e"}
s4 = s1 ^ s2 # 返回 {1,2,4,5,"a","b","d","e"}

三、集合的应用

1、去重应用

在数据清洗和整理过程中,经常需要去重操作以保证数据的准确性和完整性。使用集合可以快速实现列表、元组和字典等数据类型的去重。

l = [1,2,2,3,4,4,4,5]
l = list(set(l)) # 返回 [1,2,3,4,5]

t = (1,2,2,3,4,4,4,5)
t = tuple(set(t)) # 返回 (1,2,3,4,5)

d = {"a":1,"b":2,"c":2,"d":3,"e":3}
d = dict((y,x) for x,y in d.items())
d = dict((x,y) for y,x in d.items()) # 返回 {"a":1,"b":2,"d":3,"e":3}

2、成员资格测试应用

在数据分析和处理过程中,需要对数据进行筛选和分类,使用集合可以快速筛选数据是否符合要求。

words = ["apple","banana","orange","pineapple"]
"apple" in set(words) # 返回True

3、数据分析应用

集合在数据分析和统计中有广泛的应用,例如实现一个统计文本中单词出现次数的程序。

text = "Python is a popular programming language. Python is used for web development, data analysis, artificial intelligence, and scientific computing."
words = text.split(" ")
word_count = {}
for word in words:
    if word not in word_count:
        word_count[word] = 0
    word_count[word] += 1

sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
for word, count in sorted_word_count:
    print(word, count)

运行结果为:

Python 2
is 2
a 1
popular 1
programming 1
language. 1
used 1
for 1
web 1
development, 1
data 1
analysis, 1
artificial 1
intelligence, 1
and 1
scientific 1
computing. 1