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中的reduce内建函数使用方法指南
Aug 31 Python
收藏整理的一些Python常用方法和技巧
May 18 Python
CentOS中使用virtualenv搭建python3环境
Jun 08 Python
Python决策树之基于信息增益的特征选择示例
Jun 25 Python
10分钟教你用Python实现微信自动回复功能
Nov 28 Python
解决Python plt.savefig 保存图片时一片空白的问题
Jan 10 Python
Django中间件基础用法详解
Jul 18 Python
python通过txt文件批量安装依赖包的实现步骤
Aug 13 Python
ipad上运行python的方法步骤
Oct 12 Python
Python迭代器iterator生成器generator使用解析
Oct 24 Python
python 读取二进制 显示图片案例
Apr 24 Python
python实现在列表中查找某个元素的下标示例
Nov 16 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 检查扩展库或函数是否可用的代码
2010/04/06 PHP
使用PHP函数scandir排除特定目录
2014/06/12 PHP
php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法
2014/12/15 PHP
图片连续滚动代码[兼容IE/firefox]
2009/06/11 Javascript
JavaScript可否多线程? 深入理解JavaScript定时机制
2012/05/23 Javascript
jquery实现树形二级菜单实例代码
2013/11/20 Javascript
js/jquery判断浏览器类型的方法小结
2015/05/12 Javascript
jQuery实现ajax无刷新分页页码控件
2017/02/28 Javascript
Webpack常见静态资源处理-模块加载器(Loaders)+ExtractTextPlugin插件
2017/06/29 Javascript
React+react-dropzone+node.js实现图片上传的示例代码
2017/08/23 Javascript
bootstrap模态框嵌套、tabindex属性、去除阴影的示例代码
2017/10/17 Javascript
小程序视频列表中视频的播放与停止的示例代码
2018/07/20 Javascript
vue 组件的封装之基于axios的ajax请求方法
2018/08/11 Javascript
jQuery访问json文件中数据的方法示例
2019/01/28 jQuery
layui.tree组件的使用以及搜索节点功能的实现
2019/09/26 Javascript
使用 Opentype.js 生成字体子集的实例代码详解
2020/05/25 Javascript
[45:59]EG vs OG 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
python使用xauth方式登录饭否网然后发消息
2014/04/11 Python
Python中文件I/O高效操作处理的技巧分享
2017/02/04 Python
简单了解python装饰器原理及使用方法
2019/12/18 Python
python爬虫开发之Request模块从安装到详细使用方法与实例全解
2020/03/09 Python
基于Django signals 信号作用及用法详解
2020/03/28 Python
Python OpenCV实现测量图片物体宽度
2020/05/27 Python
Python限制内存和CPU使用量的方法(Unix系统适用)
2020/08/04 Python
一款纯css3实现的非常实用的鼠标悬停特效演示
2014/11/05 HTML / CSS
GOOD AMERICAN官网:为曲线性感而设计
2017/12/28 全球购物
意大利灯具购物网站:Lampade.it
2018/10/18 全球购物
Craghoppers德国官网:户外和旅行服装
2020/02/14 全球购物
葡萄牙语专业个人求职信
2013/12/10 职场文书
新闻传媒系求职信范文
2014/04/19 职场文书
学生会竞选演讲稿
2014/04/24 职场文书
党性分析自查总结
2014/10/14 职场文书
幼儿园亲子活动通知
2015/04/24 职场文书
奖励申请报告范文
2015/05/15 职场文书
详解MindSpore自定义模型损失函数
2021/06/30 Python
Pyqt5将多个类组合在一个界面显示的完整示例
2021/09/04 Python