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 相关文章推荐
Python3基础之list列表实例解析
Aug 13 Python
Python中用altzone()方法处理时区的教程
May 22 Python
python实现井字棋游戏
Mar 30 Python
matplotlib作图添加表格实例代码
Jan 23 Python
python使用Matplotlib画条形图
Mar 25 Python
Python使用sklearn实现的各种回归算法示例
Jul 04 Python
提升Python效率之使用循环机制代替递归函数
Jul 23 Python
django ManyToManyField多对多关系的实例详解
Aug 09 Python
在pycharm中为项目导入anacodna环境的操作方法
Feb 12 Python
Python使用GitPython操作Git版本库的方法
Feb 29 Python
面向新手解析python Beautiful Soup基本用法
Jul 11 Python
python如何实现DES加密
Sep 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验证码代码
2012/02/27 PHP
php file_get_contents抓取Gzip网页乱码的三种解决方法
2013/11/12 PHP
PHP字符串中特殊符号的过滤方法介绍
2014/02/18 PHP
php不使用插件导出excel的简单方法
2014/03/04 PHP
WordPress中用于更新伪静态规则的PHP代码实例讲解
2015/12/18 PHP
yii2.0整合阿里云oss的示例代码
2017/09/19 PHP
原生javascript获取元素样式属性值的方法
2010/12/25 Javascript
基于jQuery的图片左右无缝滚动插件
2012/05/23 Javascript
js图片自动切换效果处理代码
2013/05/07 Javascript
javascript十六进制及二进制转化的方法
2015/05/06 Javascript
javascript引用类型之时间Date和数组Array
2015/08/27 Javascript
三种Node.js写文件的方式
2016/03/08 Javascript
JS简单实现数组去重的方法示例
2017/03/27 Javascript
学习使用Bootstrap页面排版样式
2017/05/11 Javascript
Angular 4依赖注入学习教程之ValueProvider的使用(七)
2017/06/04 Javascript
jquery实现一个全局计时器(商城可用)
2017/06/30 jQuery
详谈ES6中的迭代器(Iterator)和生成器(Generator)
2017/07/31 Javascript
anime.js 实现带有描边动画效果的复选框(推荐)
2017/12/24 Javascript
结合Vue控制字符和字节的显示个数的示例
2018/05/17 Javascript
vuejs router history 配置到iis的方法
2018/09/20 Javascript
关于layui toolbar和template的结合使用方法
2019/09/19 Javascript
js脚本中执行java后台代码方法解析
2019/10/11 Javascript
Vue+ElementUI table实现表格分页
2019/12/14 Javascript
Webpack设置环境变量的一些误区详解
2019/12/19 Javascript
angular中的post请求处理示例详解
2020/06/30 Javascript
jQuery 动态粒子效果示例代码
2020/07/07 jQuery
[38:21]2018DOTA2亚洲邀请赛3月30日 小组赛A组 LGD VS Newbee
2018/03/31 DOTA
Python最基本的输入输出详解
2015/04/25 Python
解读! Python在人工智能中的作用
2017/11/14 Python
python实现简单多人聊天室
2018/12/11 Python
Python使用selenium + headless chrome获取网页内容的方法示例
2019/10/16 Python
python -v 报错问题的解决方法
2020/09/15 Python
利用css3制作3D样式按钮实现代码
2013/03/18 HTML / CSS
小学生运动会报道稿
2014/09/12 职场文书
有关骆驼祥子的读书笔记
2015/06/26 职场文书
用position:sticky完美解决小程序吸顶问题的实现方法
2021/04/24 HTML / CSS