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参数和作用域的使用
Nov 01 Python
Python实现端口复用实例代码
Jul 03 Python
Python实现的多线程端口扫描工具分享
Jan 21 Python
Python实现基于二叉树存储结构的堆排序算法示例
Dec 08 Python
python批量修改图片后缀的方法(png到jpg)
Oct 25 Python
python实现多张图片拼接成大图
Jan 15 Python
说说如何遍历Python列表的方法示例
Feb 11 Python
pytorch神经网络之卷积层与全连接层参数的设置方法
Aug 18 Python
基于Python实现拆分和合并GIF动态图
Oct 22 Python
在Django下创建项目以及设置settings.py教程
Dec 03 Python
Python中zipfile压缩文件模块的基本使用教程
Jun 14 Python
详解Flask前后端分离项目案例
Jul 24 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 getsiteurl()函数
2009/09/05 PHP
php is_file 判断给定文件名是否为一个正常的文件
2010/05/10 PHP
php使用gearman进行任务分发操作实例详解
2020/02/26 PHP
自动生成文章摘要的代码[JavaScript 版本]
2007/03/20 Javascript
jquery 经典动画菜单效果代码
2010/01/26 Javascript
Chrome中模态对话框showModalDialog返回值问题的解决方法
2010/05/25 Javascript
基于jquery的DIV随滚动条滚动而滚动的代码
2012/07/20 Javascript
javascript实现在指定元素中垂直水平居中
2015/09/13 Javascript
详解自动生成博客目录案例
2016/12/09 Javascript
微信小程序模板之分页滑动栏
2017/02/10 Javascript
JS html时钟制作代码分享
2017/03/03 Javascript
bootstrap弹出层的多种触发方式
2017/05/10 Javascript
Angular+Node生成随机数的方法
2017/06/16 Javascript
Vue 项目中遇到的跨域问题及解决方法(后台php)
2018/03/28 Javascript
webuploader实现上传图片到服务器功能
2018/08/16 Javascript
JavaScript实现4位随机验证码的生成
2021/01/28 Javascript
[44:50]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 TNC vs VG
2018/04/02 DOTA
在Django中限制已登录用户的访问的方法
2015/07/23 Python
详解Python的Django框架中的中间件
2015/07/24 Python
Windows下使Python2.x版本的解释器与3.x共存的方法
2015/10/25 Python
详谈Python基础之内置函数和递归
2017/06/21 Python
人生苦短我用python python如何快速入门?
2018/03/12 Python
python抓取京东小米8手机配置信息
2018/11/13 Python
详解python中的Turtle函数库
2018/11/19 Python
Python3中lambda表达式与函数式编程讲解
2019/01/14 Python
python实现飞机大战游戏
2020/10/26 Python
CSS3对背景图片的裁剪及尺寸和位置的设定方法
2016/03/07 HTML / CSS
使用html5制作loading图的示例
2014/04/14 HTML / CSS
阿迪达斯加拿大官网:Adidas加拿大
2016/08/25 全球购物
柏林通行证:Berlin Pass
2018/04/11 全球购物
德国家具折扣店:POCO
2020/02/28 全球购物
局域网标准
2016/09/10 面试题
干部个人对照检查材料
2014/08/25 职场文书
标准离婚协议书(2014版)
2014/10/05 职场文书
python 中[0]*2与0*2的区别说明
2021/05/10 Python
box-shadow单边阴影的实现
2023/05/21 HTML / CSS