python集合用法实例分析
作者:不吃皮蛋 发布时间:2021-10-26 23:36:37
标签: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)
# 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程序设计有所帮助。


猜你喜欢
- Django 对各种数据库提供了很好的支持,包括:PostgreSQL、MySQL、SQLite、Oracle。Django 为这些数据库提
- 不固定参数函数在go语言中,允许对函数设置不固定参数。不过需要注意的是,虽然不限制参数数量,但限制了参数的数据类型。从原理分析,不固定参数利
- 1.使用explain语句去查看分析结果 如explain select * from test1 where id=1;会出现:id se
- 下载依赖首先需要下载hadoop和spark,解压,然后设置环境变量。hadoop清华源下载spark清华源下载HADOOP_HOME =&
- Qt中正确的设置窗体的背景图片的方法大致有两种,下面将逐个讲解:一. 利用styleSheet设置窗体的背景图片使用stylesheet设置
- 这篇文章主要介绍了Python 实现两个矩形重合面积代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 本文实例讲述了golang简单获取上传文件大小的方法。分享给大家供大家参考,具体如下:package mainimport ( &
- 所有平台的Mysql下载地址为:MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台。 
- OpenAI 是一个人工智能的工具包,包括神经网络、遗传算法和有限状态机等。使用python可以非常便捷的操作OpenAI的API。一下是O
- 前言前一篇autoit实现文件上传打包成.exe可执行文件后,每次只能传固定的那个图片,我们实际测试时候希望传不同的图片。这样每次调用的时候
- 前面的话发布一个版本时,我们通常先在版本库中打一个标签(tag)。这样,就唯一确定了打标签时刻的版本。将来无论什么时候,取某个标签的版本,就
- 为了处理根据Web标准创作的网页和根据盛行于20世纪90年代末的旧时实践创作的网页,当代的Web浏览器实现了各种不同的引擎模式。本文说明了那
- 实例如下所示:>>> import pandas as pd>>> import numpy as np
- 一 位置传递没什么好过多讲解.# 位置传递实例:def fun1(a,b,c): return a+b+cprint(fun1(
- 环境配置pip install pyqt5 (安装pyQT5库):pyuic5包含在内pip install pyqt5-tools (安装
- 以前在一个图书类网站看到这样一个功能:客户可以按条件搜索书目的信息,服务器会将符合条件的信息筛选出来保存为一个Excel文件供客户下载。今天
- 使用pycharm的时候,有时需要重命名文件,该怎么操作呢?下面小编给大家演示一下。首先准备一个要重命名的文件,如下图所示接着右键单击选择R
- 下面就是我们的authenticate.asp页面,在这里,将用户的信息收集起来,连同最初的URL一起传到一个识别用户身份的页面中。我们可用
- Python可以使用 xml.etree.ElementTree 模块从简单的XML文档中提取数据。 为了演示,假设你想解析Planet P
- 微软昨天在其2009年专业开发者大会上展示了下一个版本的Internet Explorer浏览器IE9。尽管只是一个早期版本,IE开发团队还