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之不要红头文件(2)
Sep 28 Python
python开发之thread线程基础实例入门
Nov 11 Python
基于python3 类的属性、方法、封装、继承实例讲解
Sep 19 Python
tensorflow入门之训练简单的神经网络方法
Feb 26 Python
python爬取各类文档方法归类汇总
Mar 22 Python
python pandas 如何替换某列的一个值
Jun 09 Python
django连接oracle时setting 配置方法
Aug 29 Python
python3爬虫中引用Queue的实例讲解
Nov 24 Python
pytorch中Schedule与warmup_steps的用法说明
May 24 Python
变长双向rnn的正确使用姿势教学
May 31 Python
Python socket如何解析HTTP请求内容
Feb 12 Python
全网非常详细的pytest配置文件
Jul 15 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环境――Appserv
2006/12/13 PHP
phpmyadmin MySQL 加密配置方法
2009/07/05 PHP
PHP下编码转换函数mb_convert_encoding与iconv的使用说明
2009/12/16 PHP
php mb_substr()函数截取中文字符串应用示例
2014/07/29 PHP
PHP面向对象程序设计之多态性的应用示例
2018/12/19 PHP
php使用scandir()函数扫描指定目录下所有文件示例
2019/06/08 PHP
YUI 读码日记之 YAHOO.lang.is*
2008/03/22 Javascript
javascript 一些用法小结
2009/09/11 Javascript
jquery 多级下拉菜单核心代码
2010/05/21 Javascript
25个非常棒的jQuery滑块插件和教程小结
2011/09/02 Javascript
javascript 获取元素样式必杀技
2014/05/04 Javascript
jQuery中:radio选择器用法实例
2015/01/03 Javascript
JS 作用域与作用域链详解
2015/04/07 Javascript
JS实现带提示的星级评分效果完整实例
2015/10/30 Javascript
vue2.0路由切换后页面滚动位置不变BUG的解决方法
2018/03/14 Javascript
详解小程序云开发攻略(解决最棘手的问题)
2019/09/30 Javascript
解决vuex数据异步造成初始化的时候没值报错问题
2019/11/13 Javascript
angular组件间通讯的实现方法示例
2020/05/07 Javascript
详解React路由传参方法汇总记录
2020/11/29 Javascript
[02:11]2014DOTA2 TI专访VG战队Fenrir:队伍气氛良好
2014/07/11 DOTA
Python之ReportLab绘制条形码和二维码的实例
2018/01/15 Python
django解决跨域请求的问题详解
2019/01/20 Python
详谈tensorflow gfile文件的用法
2020/02/05 Python
基于python实现图片转字符画代码实例
2020/09/04 Python
Python引入多个模块及包的概念过程解析
2020/09/21 Python
关于老式浏览器兼容HTML5和CSS3的问题
2016/06/01 HTML / CSS
Java的基础面试题附答案
2016/01/10 面试题
如何写一个Java类既可以用作applet也可以用作java应用
2016/01/18 面试题
会计员岗位职责
2014/03/15 职场文书
秋季开学典礼主持词
2014/03/19 职场文书
公司大门门卫岗位职责
2014/06/11 职场文书
史上最牛的辞职信
2015/02/28 职场文书
社区环境卫生倡议书
2015/04/29 职场文书
2016年社区中秋节活动总结
2016/04/05 职场文书
详解MySQL 用户权限管理
2021/04/20 MySQL
php中配置文件保存修改操作 如config.php文件的读取修改等操作
2021/05/12 PHP