python集合用法实例分析
本文实例讲述了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
希望本文所述对大家的Python程序设计有所帮助。

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Linux 시스템과 함께 제공되는 Python 통역사를 제거하는 문제와 관련하여 많은 Linux 배포판이 설치 될 때 Python 통역사를 사전 설치하고 패키지 관리자를 사용하지 않습니다 ...

Pylance 유형 감지 문제 솔루션 Python 프로그래밍에서 사용자 정의 데코레이터를 사용할 때 Decorator는 행을 추가하는 데 사용할 수있는 강력한 도구입니다 ...

Pythonasyncio에 대해 ...

Linux 터미널에서 Python 사용 ...

Python 3.6에 피클 파일 로딩 3.6 환경 오류 : ModulenotFounderRor : nomodulename ...

파이썬 비동기 라이브러리 사이의 호환성 문제 파이썬에서 비동기 프로그래밍은 동시성과 I/O의 프로세스가되었습니다 ...

Python 3.6에 피클 파일로드 3.6 환경 보고서 오류 : modulenotfounderror : nomodulename ...

아동 프로세스의 문제와 해결책은 신호를 사용하여 부모 프로세스를 죽일 때 계속 실행됩니다. Python 프로그래밍에서 신호를 통해 부모 프로세스를 죽인 후에도 아동 프로세스는 여전히 ...
