python集合用法实例分析


Posted in Python onMay 30, 2015

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
Python urllib模块urlopen()与urlretrieve()详解
Nov 01 Python
python使用datetime模块计算各种时间间隔的方法
Mar 24 Python
Python读取和处理文件后缀为.sqlite的数据文件(实例讲解)
Jun 27 Python
python正则表达式及使用正则表达式的例子
Jan 22 Python
python实现彩色图转换成灰度图
Jan 15 Python
解决Pandas的DataFrame输出截断和省略的问题
Feb 08 Python
Python将字符串常量转化为变量方法总结
Mar 17 Python
详解Python给照片换底色(蓝底换红底)
Mar 22 Python
python中自带的三个装饰器的实现
Nov 08 Python
PyTorch加载预训练模型实例(pretrained)
Jan 17 Python
通过python调用adb命令对App进行性能测试方式
Apr 23 Python
Django-silk性能测试工具安装及使用解析
Nov 28 Python
基于wxpython实现的windows GUI程序实例
May 30 #Python
python简单实现旋转图片的方法
May 30 #Python
Python实现控制台输入密码的方法
May 29 #Python
python删除过期文件的方法
May 29 #Python
Python的Django框架中TEMPLATES项的设置教程
May 29 #Python
编写Python脚本把sqlAlchemy对象转换成dict的教程
May 29 #Python
Python fileinput模块使用实例
May 28 #Python
You might like
PHP simple_html_dom.php+正则 采集文章代码
2009/12/24 PHP
php 地区分类排序算法
2013/07/01 PHP
PHP file_get_contents函数读取远程数据超时的解决方法
2015/05/13 PHP
php集成动态口令认证
2016/07/21 PHP
Yii2 hasOne(), hasMany() 实现三表关联的方法(两种)
2017/02/15 PHP
js类中的公有变量和私有变量
2008/07/24 Javascript
jquery 子窗口操作父窗口的代码
2009/09/21 Javascript
javascript开发技术大全-第3章 js数据类型
2011/07/03 Javascript
深入理解JavaScript系列(22):S.O.L.I.D五大原则之依赖倒置原则DIP详解
2015/03/05 Javascript
jQuery实现动态表单验证时文本框抖动效果完整实例
2015/08/21 Javascript
详解JavaScript基于面向对象之创建对象(2)
2015/12/10 Javascript
JavaScript 实现的 zip 压缩和解压缩工具包Zip.js使用详解
2015/12/14 Javascript
JS Select下拉框(支持输入模糊查询)
2017/02/04 Javascript
JavaScript实现三级级联特效
2017/11/05 Javascript
vue组件父子间通信之综合练习(聊天室)
2017/11/07 Javascript
vue 列表页跳转详情页获取id以及详情页通过id获取数据
2019/03/27 Javascript
vue在路由中验证token是否存在的简单实现
2019/11/11 Javascript
node.js 使用 net 模块模拟 websocket 握手进行数据传递操作示例
2020/02/11 Javascript
Vue中ref和$refs的介绍以及使用方法示例
2021/01/11 Vue.js
[56:56]VG vs LGD 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.22
2019/09/05 DOTA
17个Python小技巧分享
2015/01/23 Python
使用Pyrex来扩展和加速Python程序的教程
2015/04/13 Python
Python利用正则表达式匹配并截取指定子串及去重的方法
2015/07/30 Python
Python中模块与包有相同名字的处理方法
2017/05/05 Python
利用scrapy将爬到的数据保存到mysql(防止重复)
2018/03/31 Python
python3+PyQt5泛型委托详解
2018/04/24 Python
解决pycharm运行出错,代码正确结果不显示的问题
2018/11/30 Python
解决python3 Pycharm上连接数据库时报错的问题
2018/12/03 Python
浅谈pycharm使用及设置方法
2019/09/09 Python
Django 请求Request的具体使用方法
2019/11/11 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
2020/08/26 Python
举例详解HTML5中使用JSON格式提交表单
2015/06/16 HTML / CSS
详解html5页面 rem 布局适配方法
2018/01/12 HTML / CSS
公民授权委托书
2014/10/15 职场文书
员工担保书范本
2015/09/22 职场文书
python机器学习实现oneR算法(以鸢尾data为例)
2022/03/03 Python