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中线程问题的简单讲解
Apr 03 Python
[原创]python爬虫(入门教程、视频教程)
Jan 08 Python
Python实现的堆排序算法示例
Apr 29 Python
Python简单爬虫导出CSV文件的实例讲解
Jul 06 Python
python过滤中英文标点符号的实例代码
Jul 15 Python
Python一键查找iOS项目中未使用的图片、音频、视频资源
Aug 12 Python
python 字符串常用函数详解
Sep 11 Python
django多种支付、并发订单处理实例代码
Dec 13 Python
基于Python实现人脸自动戴口罩系统
Feb 06 Python
python datetime处理时间小结
Apr 16 Python
基于Python爬取fofa网页端数据过程解析
Jul 13 Python
python中np是做什么的
Jul 21 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打造属于自己的MVC框架
2012/03/07 PHP
jQuery+php实现ajax文件即时上传的详解
2013/06/17 PHP
php读取文件内容的几种方法详解
2013/06/26 PHP
php正则修正符用法实例详解
2016/12/29 PHP
Yii2下点击验证码的切换实例代码
2017/03/14 PHP
PHPStudy下如何为Apache安装SSL证书的方法步骤
2019/01/23 PHP
动态加载js文件 document.createElement
2006/10/14 Javascript
jQuery最佳实践完整篇
2011/08/20 Javascript
ie与ff下的event事件使用介绍
2013/11/25 Javascript
jquery中的on方法使用介绍
2013/12/29 Javascript
JavaScript调用ajax获取文本文件内容实现代码
2014/03/28 Javascript
JS倒计时代码汇总
2014/11/25 Javascript
jquery 操作css样式、位置、尺寸方法汇总
2014/11/28 Javascript
JavaScript动态添加style节点的方法
2015/06/09 Javascript
JavaScript实现自动切换图片代码
2016/10/11 Javascript
jQuery实现动态删除LI的方法
2017/05/30 jQuery
Webpack优化配置缩小文件搜索范围
2017/12/25 Javascript
vue.js项目nginx部署教程
2018/04/05 Javascript
ES6知识点整理之模块化的应用详解
2019/04/15 Javascript
vue全局使用axios的操作
2020/09/08 Javascript
[01:15:29]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#2Secret VS EG第三局
2016/03/04 DOTA
Python多线程扫描端口代码示例
2018/02/09 Python
python3解析库pyquery的深入讲解
2018/06/26 Python
kafka-python批量发送数据的实例
2018/12/27 Python
PyQt5笔记之弹出窗口大全
2019/06/20 Python
django2笔记之路由path语法的实现
2019/07/17 Python
Python实现列表中非负数保留,负数转化为指定的数值方式
2020/06/04 Python
python中的插入排序的简单用法
2021/01/19 Python
x-ua-compatible content=”IE=7, IE=9″意思理解
2013/07/22 HTML / CSS
Pedro官网:新加坡时尚品牌
2019/08/27 全球购物
Windows和Linux动态库应用异同
2016/07/28 面试题
夜大自我鉴定
2013/10/31 职场文书
三年级小学生评语
2014/04/22 职场文书
2016年庆祝六一儿童节活动总结
2016/04/06 职场文书
详解JS ES6编码规范
2021/05/07 Javascript
python之json文件转xml文件案例讲解
2021/08/07 Python