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 线程的暂停, 恢复, 退出详解及实例
Dec 06 Python
python数字图像处理之骨架提取与分水岭算法
Apr 27 Python
Empty test suite.(PyCharm程序运行错误的解决方法)
Nov 30 Python
利用Python半自动化生成Nessus报告的方法
Mar 19 Python
Pandas中Series和DataFrame的索引实现
Jun 27 Python
python绘制双Y轴折线图以及单Y轴双变量柱状图的实例
Jul 08 Python
python-OpenCV 实现将数组转换成灰度图和彩图
Jan 09 Python
python nohup 实现远程运行不宕机操作
Apr 16 Python
Python基于requests实现模拟上传文件
Apr 21 Python
Python logging模块异步线程写日志实现过程解析
Jun 30 Python
Pycharm2020.1安装无法启动问题即设置中文插件的方法
Aug 07 Python
实例详解Python的进程,线程和协程
Mar 13 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过滤危险html代码
2008/08/18 PHP
PHP学习之正则表达式
2011/04/17 PHP
php修改时间格式的代码
2011/05/29 PHP
教你如何使用php session
2013/10/28 PHP
无需数据库在线投票调查php代码
2016/07/20 PHP
彻底搞懂PHP 变量结构体
2017/10/11 PHP
node.js中的console.warn方法使用说明
2014/12/09 Javascript
javascript实现详细时间提醒信息效果的方法
2015/03/11 Javascript
node.js入门实例helloworld详解
2015/12/23 Javascript
KVM虚拟化技术之使用Qemu-kvm创建和管理虚拟机的方法
2016/10/05 Javascript
利用Node.js对文件进行重命名
2017/03/12 Javascript
基于jQuery的表单填充实例
2017/08/22 jQuery
vue中的模态对话框组件实现过程
2018/05/01 Javascript
详解离线安装npm包的几种方法
2018/11/25 Javascript
React中使用外部样式的3种方式(小结)
2019/05/28 Javascript
vue实现图片上传预览功能
2019/12/23 Javascript
[00:12]DAC2018 no[o]ne亮相SOLO赛 他是否如他的id一样无人可挡?
2018/04/06 DOTA
[43:47]完美世界DOTA2联赛PWL S3 LBZS vs Phoenix 第一场 12.09
2020/12/11 DOTA
python如何实现excel数据添加到mongodb
2015/07/30 Python
python 打印出所有的对象/模块的属性(实例代码)
2016/09/11 Python
Python 操作文件的基本方法总结
2017/08/10 Python
Python3 模块、包调用&路径详解
2017/10/25 Python
pygame游戏之旅 添加碰撞效果的方法
2018/11/20 Python
Python读取指定日期邮件的实例
2019/02/01 Python
python实现维吉尼亚算法
2019/03/20 Python
Python中变量的输入输出实例代码详解
2019/07/28 Python
pytorch中tensor.expand()和tensor.expand_as()函数详解
2019/12/27 Python
python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法
2020/11/05 Python
Python离线安装各种库及pip的方法
2020/11/28 Python
naturalizer加拿大官网:美国娜然女鞋
2017/04/04 全球购物
聚美优品广告词改编
2014/03/14 职场文书
元旦趣味活动方案
2014/08/22 职场文书
三傻大闹宝莱坞观后感
2015/06/03 职场文书
Django rest framework如何自定义用户表
2021/06/09 Python
使用logback实现按自己的需求打印日志到自定义的文件里
2021/08/30 Java/Android
Matplotlib绘制条形图的方法你知道吗
2022/03/21 Python