python集合用法實(shí)例分析
本文實(shí)例講述了python集合用法。分享給大家供大家參考。具體分析如下:
# sets are unordered collections of unique hashable elements # Python23 tested vegaseat 09mar2005 # Python v2.4 has sets built in import sets print "List the functions within module 'sets':" for funk in dir(sets): print funk # create an empty set set1 = set([]) # now load the set for k in range(10): set1.add(k) print "\nLoaded a set with 0 to 9:" print set1 set1.add(7) print "Tried to add another 7, but it was already there:" print set1 # make a list of fruits as you put them into a basket basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] print "\nThe original list of fruits:" print basket # create a set from the list, removes the duplicates fruits = sets.Set(basket) print "\nThe set is unique, but the order has changed:" print fruits # let's get rid of some duplicate words str1 = "Senator Strom Thurmond dressed as as Tarzan" print "\nOriginal string:" print str1 print "A list of the words in the string:" wrdList1 = str1.split() print wrdList1 # now create a set of unique words strSet = sets.Set(wrdList1) print "The set of the words in the string:" print strSet print "Convert set back to string (order has changed!):" print " ".join(strSet) print # comparing two sets, bear with me ... colorSet1 = sets.Set(['red','green','blue','black','orange','white']) colorSet2 = sets.Set(['black','maroon','grey','blue']) print "colorSet1 =", colorSet1 print "colorSet2 =", colorSet2 # same as (colorSet1 - colorSet2) colorSet3 = colorSet1.difference(colorSet2) print "\nThese are the colors in colorSet1 that are not in colorSet2:" print colorSet3 # same as (colorSet1 | colorSet2) colorSet4 = colorSet1.union(colorSet2) print "\nThese are the colors appearing in both sets:" print colorSet4 # same as (colorSet1 ^ colorSet2) colorSet5 = colorSet1.symmetric_difference(colorSet2) print "\nThese are the colors in colorSet1 or in colorSet2, but not both:" print colorSet5 # same as (colorSet1 & colorSet2) colorSet6 = colorSet1.intersection(colorSet2) print "\nThese are the colors common to colorSet1 and colorSet2:" print colorSet6
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python+Pygame實(shí)現(xiàn)簡(jiǎn)單的射擊小游戲
要說什么游戲能夠獲得大家的喜愛,唯射擊游戲莫屬!本文將利用Python和Pygame庫制作一個(gè)簡(jiǎn)單的射擊小游戲,感興趣的小伙伴可以了解一下2022-04-04跟老齊學(xué)Python之大話題小函數(shù)(2)
上篇文章我們講訴了map 和lambda函數(shù)的使用,本文我們繼續(xù)來看看reduce和filter函數(shù),有需要的朋友可以參考下2014-10-10python實(shí)現(xiàn)進(jìn)度條和系統(tǒng)通知的示例詳解
這篇文章主要和大家分享兩個(gè)有意思的Python小工具,可以優(yōu)雅地實(shí)現(xiàn)進(jìn)度條和系統(tǒng)通知,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴快也跟隨小編一起學(xué)習(xí)一下2023-11-11python自動(dòng)化測(cè)試工具Helium使用示例
大家好,本篇文章主要講的是python自動(dòng)化測(cè)試工具Helium使用示例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下哦2021-12-12Django 配置多站點(diǎn)多域名的實(shí)現(xiàn)步驟
這篇文章主要介紹了Django 配置多站點(diǎn)多域名的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Python實(shí)現(xiàn)免費(fèi)音樂下載器
本文主要為大家介紹了通過Python實(shí)現(xiàn)的免費(fèi)音樂下載器,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的小伙伴可以學(xué)習(xí)一下2021-12-12Python進(jìn)階學(xué)習(xí)之你真的懂元組嗎?
在我們學(xué)習(xí)python的過程中,對(duì)元組的介紹通常是成為”不可變的列表“,但是這其實(shí)并沒有完全的概括元組的功能。在本文中,我們將會(huì)介紹元組作為記錄的功能,話不多說我們開始吧2023-04-04Python搭建HTTP服務(wù)器和FTP服務(wù)器
這篇文章主要為大家詳細(xì)介紹了Python搭建HTTP服務(wù)器和FTP服務(wù)器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03