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的*args和**kwargs用法
Nov 01 Python
pandas 取出表中一列数据所有的值并转换为array类型的方法
Apr 11 Python
对python指数、幂数拟合curve_fit详解
Dec 29 Python
python使用Qt界面以及逻辑实现方法
Jul 10 Python
对python 树状嵌套结构的实现思路详解
Aug 09 Python
TensorFlow实现从txt文件读取数据
Feb 05 Python
pyqt5中动画的使用详解
Apr 01 Python
图解Python中深浅copy(通俗易懂)
Sep 03 Python
python 删除系统中的文件(按时间,大小,扩展名)
Nov 19 Python
python 调整图片亮度的示例
Dec 03 Python
Python基础学习之奇异的GUI对话框
May 27 Python
python自动获取微信公众号最新文章的实现代码
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模拟js函数unescape的函数代码
2012/10/20 PHP
一个简洁实用的PHP缓存类完整实例
2014/07/26 PHP
PHP封装的完整分页类示例
2018/08/21 PHP
Jquery Ajax学习实例4 向WebService发出请求,返回实体对象的异步调用
2010/03/16 Javascript
js实现的日期操作类DateTime函数代码
2010/03/16 Javascript
利用jQuery插件扩展识别浏览器内核与外壳的类型和版本的实现代码
2011/10/22 Javascript
jQuery实现新消息在网页标题闪烁提示
2015/06/23 Javascript
jQuery实现列表内容的动态载入特效
2015/08/08 Javascript
整理Javascript基础入门学习笔记
2015/11/29 Javascript
jQuery ajax全局函数处理session过期后的ajax跳转问题
2016/06/03 Javascript
javascript操作cookie
2017/01/17 Javascript
Bootstrap fileinput文件上传预览插件使用详解
2017/05/16 Javascript
angularjs封装$http为factory的方法
2017/05/18 Javascript
详解Layer弹出层样式
2017/08/21 Javascript
基于 D3.js 绘制动态进度条的实例详解
2018/02/26 Javascript
实例分析vue循环列表动态数据的处理方法
2018/09/28 Javascript
详解Vue项目部署遇到的问题及解决方案
2019/01/11 Javascript
详解Next.js页面渲染的优化方案
2019/01/27 Javascript
微信小程序实现渐入渐出动画效果
2019/06/13 Javascript
在博客园博文中添加自定义右键菜单的方法详解
2020/02/05 Javascript
Vue实现图书管理小案例
2020/12/03 Vue.js
用Python进行基础的函数式编程的教程
2015/03/31 Python
Python文件去除注释的方法
2015/05/25 Python
Windows下实现Python2和Python3两个版共存的方法
2015/06/12 Python
Python编写合并字典并实现敏感目录的小脚本
2019/02/26 Python
简单了解django orm中介模型
2019/07/30 Python
使用Django实现把两个模型类的数据聚合在一起
2020/03/28 Python
CSS3模块的目前的状况分析
2010/02/24 HTML / CSS
DBA数据库管理员JAVA程序员架构师必看
2016/02/07 面试题
大三学生入党思想汇报
2014/01/02 职场文书
企业管理标语
2014/06/10 职场文书
建筑院校毕业生求职信
2014/06/13 职场文书
民政局副局长民主生活会个人整改措施
2014/10/04 职场文书
病人慰问信范文
2015/02/15 职场文书
高二化学教学反思
2016/02/22 职场文书
Mysql分库分表之后主键处理的几种方法
2022/02/15 MySQL