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黑魔法之参数传递
Feb 12 Python
深入理解NumPy简明教程---数组1
Dec 17 Python
Django入门使用示例
Dec 12 Python
python实现excel读写数据
Mar 02 Python
使用Selenium破解新浪微博的四宫格验证码
Oct 19 Python
pandas pivot_table() 按日期分多列数据的方法
Nov 16 Python
基于Python的PIL库学习详解
May 10 Python
Python Django 简单分页的实现代码解析
Aug 21 Python
opencv python Canny边缘提取实现过程解析
Feb 03 Python
K最近邻算法(KNN)---sklearn+python实现方式
Feb 24 Python
pyCharm 设置调试输出窗口中文显示方式(字符码转换)
Jun 09 Python
python机器学习实现oneR算法(以鸢尾data为例)
Mar 03 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+dbfile开发小型留言本
2006/10/09 PHP
PHP base64编码后解码乱码的解决办法
2014/06/19 PHP
ThinkPHP的Widget扩展实例
2014/06/19 PHP
分享一个Laravel好用的Cache宏
2015/03/02 PHP
php+ajax实现无刷新动态加载数据技术
2015/04/28 PHP
[JS]点出统计器
2020/10/11 Javascript
jquery.combobox中文api和例子,修复了上面的小bug
2011/03/28 Javascript
jquery 判断滚动条到达了底部和顶端的方法
2014/04/02 Javascript
12 款 JS 代码测试必备工具(翻译)
2016/12/13 Javascript
关于Angular2 + node接口调试的解决方案
2017/05/28 Javascript
js Dom实现换肤效果
2017/10/21 Javascript
setTimeout时间设置为0详细解析
2018/03/13 Javascript
jQuery轻量级表单模型验证插件
2018/10/15 jQuery
JS继承定义与使用方法简单示例
2020/02/19 Javascript
[46:59]完美世界DOTA2联赛PWL S2 GXR vs Ink 第二场 11.19
2020/11/20 DOTA
python网页请求urllib2模块简单封装代码
2014/02/07 Python
跟老齐学Python之有点简约的元组
2014/09/24 Python
使用Python编写简单的画图板程序的示例教程
2015/12/08 Python
Python的Flask站点中集成xhEditor文本编辑器的教程
2016/06/13 Python
python3利用smtplib通过qq邮箱发送邮件方法示例
2017/12/03 Python
Python从数据库读取大量数据批量写入文件的方法
2018/12/10 Python
Django如何自定义model创建数据库索引的顺序
2019/06/20 Python
keras slice layer 层实现方式
2020/06/11 Python
利用PyQt5+Matplotlib 绘制静态/动态图的实现代码
2020/07/13 Python
python连接mysql数据库并读取数据的实现
2020/09/25 Python
SmartBuyGlasses美国官网:太阳眼镜和眼镜
2017/08/20 全球购物
nohup的用法
2012/11/26 面试题
入党自我鉴定
2014/03/25 职场文书
法人委托书范本
2014/04/04 职场文书
教育合作协议范本
2014/10/17 职场文书
2014流动人口计划生育工作总结
2014/12/20 职场文书
煤矿安全保证书
2015/02/27 职场文书
工程资料员岗位职责
2015/04/13 职场文书
python通过函数名调用函数的几种方法总结
2021/06/07 Python
SpringBoot项目中控制台日志的保存配置操作
2021/06/18 Java/Android
Nginx 路由转发和反向代理location配置实现
2021/11/11 Servers