1. 程式人生 > >python 內建資料結構的基本操作 —— Set(1)

python 內建資料結構的基本操作 —— Set(1)

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.

Here is a brief demonstration:

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)                      # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket                 # fast membership testing
True >>> 'crabgrass' in basket False >>> # Demonstrate set operations on unique letters from two words ... >>> a = set('abracadabra') >>> b = set('alacazam') >>> a # unique letters in a {'a', 'r', 'b', 'c', 'd'} >>>
a - b # letters in a but not in b {'r', 'd', 'b'} >>> a | b # letters in either a or b {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} >>> a & b # letters in both a and b {'a', 'c'} >>> a ^ b # letters in a or b but not both {'r', 'd', 'b', 'm', 'z', 'l'}

Similarly to list comprehensions, set comprehensions are also supported:

>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}

相關推薦

python 資料結構基本操作 —— Set1

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membersh

python 資料結構基本操作 —— tuple1

We saw that lists and strings have many common properties, such as indexing and slicing operations. They are two examples of sequen

python 資料結構基本操作 —— dict2

A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapp

python 資料結構基本操作 —— list2

The list data type has some more methods. Here are all of the methods of list objects: list.append(x) Add an item to the end of t

python - 資料結構總結

python資料型別: 數值型別 bool布林型別 str字串型別 list列表型別 tuple元組型別 set集合型別 dict字典型別 可變資料型別 不可變資料型別: 1.可變資料型別:list set dict(是否可以增刪改查) 2.不可變資料型別:數值 bool str tup

python資料結構heapq【以後補充&修正】

heapq 最小堆 heapq.heapify()將列表原地轉換為堆。 sort()區別在於heap採用的是堆排序演算法,sort採用的是歸併排序演算法。 堆(heap)是一個樹形資料結構,其中子節點與父節點是一種有序關係。 二叉堆(Binary heap)可以使用以

python-資料結構

漢諾塔問題 規則: 每次移動一個盤子 任何時候大盤子在下面,小盤子在上面 方法: n=1: 直接把A上的一個盤子移動到C上, A->C n=2: 把小盤子從A放到B上, A->B

python資料結構

分類 數值型 int、float、complex、bool 數列物件 字串 str 列表 list tuple 鍵值對 集合set 字典dict 數值型 數值型 int、floa

Python小知識-序列資料結構之集合set

這篇文章講的是Python的集合set型別 set集合簡介 集合是一個無序的(類似無序的還有字典),不重複的資料集合。其基本功能包括下面兩種: 去重:把一個還有重複元素的列表或元組等資料型別轉變成集合,其中的重複元素只出現一次。使用set()方法。 進行關係測試:測

資料結構之連結串列1:單鏈表基本操作

1.前言 1.1宣告 文章中的文字可能存在語法錯誤以及標點錯誤,請諒解; 如果在文章中發現程式碼錯誤或其它問題請告知,感謝! 2.關於連結串列 2.1什麼是連結串列 連結串列可以看成一種在物理儲存單元上的非連續、非順序儲存的資料結構,該資

資料結構實驗一:1順序表線性表的各種操作SqList

#include<iostream> #include<malloc.h> #include<cstdio> #define MAXSIZE 10 using namespace std; //typedef struct SqList

資料結構-二叉樹1以及前序、中序、後序遍歷python實現

上篇文章我們介紹了樹的概念,今天我們來介紹一種特殊的樹——二叉樹,二叉樹的應用很廣,有很多特性。今天我們一一來為大家介紹。 二叉樹 顧名思義,二叉樹就是隻有兩個節點的樹,兩個節點分別為左節點和右節點,特別強調,即使只有一個子節點也要區分它是左節點還是右節點。 常見的二叉樹有一般二叉樹、完全二叉樹、滿二叉樹、線

資料結構與演算法入門1

一、資料結構 資料之間相互存在的一種或多種特定的關係的元素的集合。 邏輯結構 資料物件中資料元素之間的相互關係 1.集合結構 在資料結構中,如果不考慮資料元素之間的關係,這種結構稱為集合結構。 各個元素是平等的,共同屬性是屬於同一個集合 2.線性結構 線性結構中的資料元素之間

資料結構——排序與查詢1——排序與查詢簡介

排序與查詢 排序,是指將一系列無序的記錄,通過某種方式或者演算法,將其變為有序的過程。如果排出來的順序是由小到大排列,我們就稱這種排序叫升序排序。如果是由大到小,我們就稱為降序排序。例如有一組資料 : 開始時為: 2 4 7 1 9 升序排序: 1 2 4 7 9 降序排序: 9 7

python學習二----資料結構

Python內建資料結構學習 1. 列表List 列表list可以實現增加,刪除,查詢操作 >>> list = ['dog','cat','mouse']; >>> list ['dog','cat','mouse'] 1-

python資料結構——字典

【引言】如果保持資料有序並不重要而結構很重要,那麼可以考慮python中的另外兩種內建的無序資料結構——字典和集合。 1.字典的概念 1.python字典允許儲存一個鍵/值對集合。在字典中每個唯一鍵有一個與之關聯的值,字典可以包含多個鍵/值對。與鍵關聯的value值可以是任意資料型別

python資料結構——列表

【引言】python提供了4個內建資料結構,可以用來儲存任何物件集合,它們分別是列表、元組、字典和集合。python有兩個有序的集合資料結構(列表和元組),列表是其中之一。 1. 列表的概念 列表是一個有序的可變物件索引集合,列表中的每個物件從0開始編號。 與陣列不同的是: 1)列表

python的四種資料結構

對於每種程式語言一般都會規定一些容器來儲存某些資料,就像java的集合和陣列一樣python也同樣有這樣的結構 而對於python他有四個這樣的內建容器來儲存資料,他們都是python語言的一部分可以直接使用而無需額外的匯入 一.列表   列表一種跟java和c中的資料很像的一種資料結構,他都是儲存一系

關於pycharm;python常用方法;資料結構

pycharm設定介面(ctrl+alt+s) 修改編輯介面字型 Flie ----> Settings ----> Editor ----> Font 修改控制檯介面字型 Flie ----> Settings ----> Ed

Python 6-1.資料結構之list(基礎篇)

-常見內建資料結構- list 列表 set 集合 dict 字典 tuple 元祖-本章大綱- list(列表): 一組由順序的資料的組合 建立列表 有值列表 無值列表(空列表