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 字符串操作实现代码(截取/替换/查找/分割)
Jun 08 Python
python实现端口转发器的方法
Mar 13 Python
python 爬取微信文章
Jan 30 Python
Python2.x利用commands模块执行Linux shell命令
Mar 11 Python
浅谈Python爬取网页的编码处理
Nov 04 Python
Bottle框架中的装饰器类和描述符应用详解
Oct 28 Python
python实现从pdf文件中提取文本,并自动翻译的方法
Nov 28 Python
Python使用修饰器进行异常日志记录操作示例
Mar 19 Python
python使用装饰器作日志处理的方法
Jul 11 Python
使用Python实现分别输出每个数组
Dec 06 Python
opencv3/C++ 平面对象识别&透视变换方式
Dec 11 Python
Python基于pyecharts实现关联图绘制
Mar 27 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
JAVA/JSP学习系列之二
2006/10/09 PHP
如何跨站抓取别的站点的页面的补充
2006/10/09 PHP
mysql中存储过程、函数的一些问题
2007/02/14 PHP
PHP学习笔记之一
2011/01/17 PHP
深入PHP获取随机数字和字母的方法详解
2013/06/06 PHP
解析使用substr截取UTF-8中文字符串出现乱码的问题
2013/06/20 PHP
php数组查找函数in_array()、array_search()、array_key_exists()使用实例
2014/04/29 PHP
学习php设计模式 php实现原型模式(prototype)
2015/12/07 PHP
JavaScript CSS 修改学习第四章 透明度设置
2010/02/19 Javascript
Javascript学习笔记之 函数篇(三) : 闭包和引用
2014/11/23 Javascript
jQuery给动态添加的元素绑定事件的方法
2015/03/09 Javascript
浅析Nodejs npm常用命令
2016/06/14 NodeJs
jQuery Plupload上传插件的使用
2017/04/19 jQuery
jQuery实现鼠标滑过商品小图片上显示对应大图片功能【测试可用】
2018/04/27 jQuery
egg.js的基本使用和调用数据库的方法示例
2019/05/18 Javascript
Vue简单实现原理详解
2020/05/07 Javascript
基于vue--key值的特殊用处详解
2020/07/31 Javascript
原生js 实现表单验证功能
2021/02/08 Javascript
python 中if else 语句的作用及示例代码
2018/03/05 Python
python 在屏幕上逐字显示一行字的实例
2018/12/24 Python
Python简易版停车管理系统
2019/08/12 Python
HTML5 Canvas中使用路径描画二阶、三阶贝塞尔曲线
2015/01/01 HTML / CSS
城市轨道专业个人求职信范文
2013/09/23 职场文书
幼儿园园长岗位职责
2013/11/26 职场文书
国贸类专业毕业生的求职信分享
2013/12/08 职场文书
优秀社区干部事迹材料
2014/02/03 职场文书
《小蝌蚪找妈妈》教学反思
2014/02/21 职场文书
餐厅采购员岗位职责
2014/03/06 职场文书
“四风”问题整改措施和努力方向
2014/09/20 职场文书
教师国庆节演讲稿范文2014
2014/09/21 职场文书
警察正风肃纪剖析材料
2014/10/16 职场文书
毕业生班级鉴定评语
2015/01/04 职场文书
火烧圆明园的观后感
2015/06/03 职场文书
小米11和iphone12哪个值得买?小米11对比iphone12评测
2021/04/21 数码科技
浅谈Redis 中的过期删除策略和内存淘汰机制
2022/04/03 Redis
pytorch实现加载保存查看checkpoint文件
2022/07/15 Python