常见数据结构-set集合

2022-08-19 15:06:12 浏览数 (1)

一、概述

特性:与dict类似,是一组key的集合(不存储value)

本质:无序和无重复的集合

二、基本使用

  • 创建
代码语言:javascript复制
<span class="hljs-comment">#创建:需要用一个list或者tuple作为输入集合</span>
s1 = set([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])
print(s1, type(s1))
s2 = set((<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>))
print(s2, type(s2))
s3 = set(<span class="hljs-string">"zutuanxue"</span>)
print(s3, type(s3))
  • 作用
代码语言:javascript复制
<span class="hljs-comment"># 作用:列表去重</span>
li1 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">5</span>,<span class="hljs-number">4</span>,<span class="hljs-number">3</span>,<span class="hljs-number">22</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">46</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>,<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">5</span>]
s4 = set(li1)
li2 = list(s4)
print(li2)
  • 添加
代码语言:javascript复制
s5 = set([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])
<span class="hljs-comment"># 不能直接插入一个数字元素</span>
<span class="hljs-comment"># s5.update(6) # 报错</span>
<span class="hljs-comment"># s5.update([6,7,8])</span>
<span class="hljs-comment"># s5.update((6,7,8))</span>
<span class="hljs-comment"># s5.update("678")</span>
s5.update([(<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>)])
print(s5)
  • 删除
代码语言:javascript复制
s6 = set([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])
<span class="hljs-comment"># 从左侧开始删除</span>
data = s6.pop()
print(data, s6)
<span class="hljs-comment"># 按元素删除,如果元素不存在报KeyError的异常</span>
s6.remove(<span class="hljs-number">4</span>)
<span class="hljs-comment"># s6.remove(7)</span>
print(s6)
  • 遍历
代码语言:javascript复制
s7 = set([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])

<span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> s7:
    print(<span class="hljs-string">"--------"</span>, key)
    
<span class="hljs-keyword">for</span> index, key <span class="hljs-keyword">in</span> enumerate(s7):
    print(index, key)

三、交集与并集

代码语言:javascript复制
s8 = set([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])
s9 = set([<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>])
<span class="hljs-comment">#交集</span>
print(s8 & s9)
<span class="hljs-comment">#并集</span>
print(s8 | s9)

0 人点赞