1. 程式人生 > >python set集合的特點,功能and常見方法

python set集合的特點,功能and常見方法

python set集合:
特點:
1》無序
2》元素不重複
功能:
1》關係測試
2》去重
常見方法:
1》集合定義
>>> s={2,3,4}
>>> type(s)
<type 'set'>
>>> s
set([2, 3, 4])
>>> s={1,2,3,2,2,1}#自動去重(集合中的元素不重複)
>>> s

set([1, 2, 3])

>>> s={1,2,3}#可以通過這樣的形式生成一個非空集合
>>> s
set([1, 2, 3])

如何生成一個空集合呢?
>>> s={}#這樣做生成的是空字典,並不是空集合
>>> s
{}
>>> type(s)
<type 'dict'>

可以這樣生成空集合
>>> s=set([])#將空列表轉化成空集合
>>> s
set([])
>>> s=set(())#也可以將空元組轉化成空集合
>>> s
set([])

2》將列表或元組轉換成集合
>>> l=range(10)
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> s=set(l)#將列表轉化成集合
>>> s
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[0]#列表有序,通過下標訪問元素
0
>>> s[0]#集合無序,不支援下標訪問
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    s[0]
TypeError: 'set' object does not support indexing
>>> t=(1,2,3,4,3,2)
>>> s=set(t)#將元組轉換成集合(自動去重)
set([1, 2, 3, 4])
3》add()方法
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l.append(9)#列表中允許存在重複元素
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9]
>>> s
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s.add(9)#新增失敗(集合中元素不允許重複)
>>> s
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s.add(10)#新增成功
>>> s
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
4》pop()方法
>>> help(set.pop)
Help on method_descriptor:
pop(...)
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.

>>> s={3,4}
>>> s
set([3, 4])
>>> s.pop()
3
>>> s.pop()
4
>>> s
set([])
>>> s.pop()
Traceback (most recent call last):
  File "<pyshell#53>", line 1, in <module>
    s.pop()

KeyError: 'pop from an empty set'

(完)

相關推薦

python set集合特點功能and常見方法

python set集合: 特點: 1》無序 2》元素不重複 功能: 1》關係測試 2》去重 常見方法: 1》集合定義 >>> s={2,3,4} >>> type(s) <type 'set'> >>> s

python set(集合) & 與 and 、 | 與 or之間的區別

直接上程式碼:a = set([1, 2, 3, 4, 5]) b = set([4, 5, 6, 7, 8]) #求兩個集合的交集 print(a & b) print(a and b) #求兩個集合的並集 print(a | b) print(a or b)主要的

python set集合的用法

常用 刪除 pan rgs 發生 **kwargs set col ear set 集合:是一組無序的不可重復的集合 1.set的創建 se={"ww3",23432,"name"} #創建一個空的集合 se1=set() 2.轉換成集合 li=[23,56,23,

python SET集合

查看 gen pan 對象 重復元素 clas n) pre pri set 的簡介:  python的set和其他語言類似, 是一個無序不重復元素集, 基本功能包括關系測試和消除重復元素. 集合對象還支持union(聯 合), intersection(交), diffe

python set集合的基本運算

凍結的集合 一般的集合set都是可原處修改的集合。還有一種集合,不能在原處修改。 這種集合的建立方法是: frozenset(“hiekay”) >>> f_set = frozenset("hiekay") #看這個名字就知道了frozen,凍結的se

python set集合 推導式

集合是一個無序不重複元素的集,基本功能包括關係測試和消除重複元素,結合物件還支援union(聯合) intersection(交)  difference(差)  sysmmerric difference(對稱差集)等數學運算 大括號或set()函式可以用來建立集合,注意

set集合特點級子類的特點

Set集合,無序,不可以重複元素 Set集合中的子類HashSet和TreeSet集合的特點 首先說一下HashSet:  資料結構是雜湊表。執行緒是非同步的。 保證元素唯一性的原理:判斷元素的has

endswith()range()append()flush() 等常見方法

Python List append()方法   描述 append() 方法用於在列表末尾新增新的物件。 語法 append()方法語法: list.append(obj) 引數 obj -- 新增到列表末尾的物件。 返回值 該方法無

2018-11-28親測有效的python執行緒終結終止執行緒方法

import ctypes def _async_raise(tid, exctype): """raises the exception, performs cleanup if needed""" if not inspect.isclass(exctype):

python-numpy.array中any()和all()方法介紹

0.摘要 本文主要介紹numpy.array.any()和numpy.array.all()的用法和區別。 1.np.array.any()和numpy.array.all() np.array.any()是或操作,將np.array中所有元素進行或操作,然後返回T

Python——詳解__slots__property和私有方法

本文始發於個人公眾號:TechFlow,原創不易,求個關注 今天是Python專題的第11篇文章,我們來聊聊面向物件的一些進階使用。 __slots__ 如果你看過github當中一些大牛的程式碼,你會發現很多大牛經常在類的頂部加上__slots__關鍵字。如果你足夠好奇,你可能會試著把這個關鍵字去掉再執行

Python set運算 集合差集並集交集list去重復

bsp 一行 color nbsp 方便 移除 line pytho 差集 在沒有發現方便的set運算之前,都是用遍歷list查找兩個集合的差別。 比如, 找list1和list2的差集 for i in list1: if not i in list2:

Python集合(set)的基本操作以及一些常見的用法

python set 集合 集合的一些常見用法 Python除了List、Tuple、Dict等常用數據類型外,還有一種數據類型叫做集合(set),集合的最大特點是:集合裏邊的元素是不可重復的並且集合內的元素還是無序的,所以一般情況下集合常用的兩個場景是:1.去重(如:列表去重);2.關

python物件資料型別Iterator與Sequece Mapping and Set的區別

前言: 迭代器都是能夠可迭代和具有__iter__屬性的 但是能夠可迭代和具有__iter__屬性的不一定就是迭代器(iterator),它比迭代器要高一個等級。 iterator(迭代器) 必須有兩種方法: next:Return the next item from

使用Python這麼多年才發現Python還有這些實用的功能特點

在使用Python多年以後,我偶然發現了一些我們過去不知道的功能和特性。一些可以說是非常有用,但卻沒有充分利用。考慮到這一點,我編輯了一些你應該瞭解的Python功能特色。 帶任意數量引數的函式 你可能已經知道了Python允許你定義可選引數。但還有一個方法,可以定義

python集合set

所有 copy clas 集合 ear hab allow span -s 1.測試 1 # python2和python3方法列表相同 2 ops23 = [‘add‘, ‘clear‘, ‘copy‘, ‘difference‘, ‘difference_upda

Python 基礎 - Day 2 Learning Note - Set 集合

基礎 差集 可變集合 自動 lap 完全 添加 key值 com 集合是一個無序的,不重復的數據組合,它的主要作用如下: 去重,把一個列表變成集合,就自動去重了 關系測試,測試兩組數據之前的交集、差集、並集等關系 SET的分為 可變集合 和 不可變集合(frozon se

python裏list列表tuple元組內部功能介紹

元素 reverse 兩個 python 位置 文件 mov xtend 排序 list列表 append #在列表尾部追加元素 clear #把列表清空 count #統計元素出現的次數 ex

Python--16 集合 在我的世界裏你就是唯一

line spa support 得到 num strong [] style 可變集合   >>> num = {}  >>> type(num)  <class ‘dict‘>  >>> num2 = {

Pythonset集合與collections系列

update common ren date 原理 symmetric pda () http 1》set集合:是一個無序且不重復的元素集合;訪問速度快,解決了重復的問題;   s2 = set(["che","liu","haha"])