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中如何优雅的合并两个字典(dict)方法示例
Aug 09 Python
Python随机生成均匀分布在单位圆内的点代码示例
Nov 13 Python
python实现kNN算法
Dec 20 Python
django解决跨域请求的问题详解
Jan 20 Python
在Pandas中处理NaN值的方法
Jun 25 Python
python 实现多线程下载m3u8格式视频并使用fmmpeg合并
Nov 15 Python
python实现简易淘宝购物
Nov 22 Python
Python 读取 YUV(NV12) 视频文件实例
Dec 09 Python
python PIL/cv2/base64相互转换实例
Jan 09 Python
Python 3.8 新功能来一波(大部分人都不知道)
Mar 11 Python
详解Flask前后端分离项目案例
Jul 24 Python
Django 实现图片上传和下载功能
Dec 31 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
建站常用13种PHP开源CMS比较
2009/08/23 PHP
解析php中反射的应用
2013/06/18 PHP
PHP 输出URL的快捷方式示例代码
2013/09/22 PHP
PHP CURL CURLOPT参数说明(curl_setopt)
2013/09/30 PHP
php+highchats生成动态统计图
2014/05/21 PHP
PHP制作用户注册系统
2015/10/23 PHP
php遍历替换目录下文件指定内容的方法
2016/11/10 PHP
微信公众平台开发教程①获取用户Openid及个人信息图文详解
2019/04/10 PHP
PHP架构及原理知识点详解
2019/12/22 PHP
基于jQuery的Tab选项框效果代码(插件)
2011/03/01 Javascript
js控制的遮罩层实例介绍
2013/05/29 Javascript
jquery.validate的使用说明介绍
2013/11/12 Javascript
JS实现图片高亮展示效果实例
2015/11/24 Javascript
JavaScript实现输入框与清空按钮联动效果
2016/09/09 Javascript
Angular使用Md5加密的解决方法
2017/09/16 Javascript
VueJs 将接口用webpack代理到本地的方法
2017/11/27 Javascript
详解如何在react中搭建d3力导向图
2018/01/12 Javascript
解决axios发送post请求返回400状态码的问题
2018/08/11 Javascript
JavaScript实现联动菜单特效
2020/01/07 Javascript
[01:18]DOTA2超级联赛专访hanci ForLove淘汰感言曝光
2013/06/04 DOTA
[01:03:31]DOTA2上海特级锦标赛B组资格赛#1 Alliance VS Fnatic第二局
2016/02/26 DOTA
python+matplotlib绘制旋转椭圆实例代码
2018/01/12 Python
python实现将字符串中的数字提取出来然后求和
2020/04/02 Python
python字典与json转换的方法总结
2020/12/28 Python
施华洛世奇英国官网:SWAROVSKI英国
2017/03/13 全球购物
ASOS英国官网:英国在线时装和化妆品零售商
2017/05/19 全球购物
美国价格实惠的在线眼镜网站:Zeelool
2020/12/25 全球购物
外企财务年会演讲稿
2014/01/03 职场文书
运动会跳远广播稿
2014/02/04 职场文书
计算机专业毕业生自荐信范文
2014/03/06 职场文书
吨的认识教学反思
2014/04/27 职场文书
抵押贷款承诺书
2014/05/30 职场文书
餐厅感恩节活动策划方案
2014/10/11 职场文书
廉政承诺书2015
2015/04/28 职场文书
班级管理经验交流材料
2015/11/02 职场文书
关于MySQL临时表为什么可以重名的问题
2022/03/22 MySQL