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使用正则表达式分析网页中的图片并进行替换的方法
Mar 26 Python
用map函数来完成Python并行任务的简单示例
Apr 02 Python
对于Python中线程问题的简单讲解
Apr 03 Python
PyQt5每天必学之布局管理
Apr 19 Python
python3使用SMTP发送简单文本邮件
Jun 19 Python
python opencv实现运动检测
Jul 10 Python
Python双向循环链表实现方法分析
Jul 30 Python
详解Python用三种方式统计词频的方法
Jul 29 Python
python并发爬虫实用工具tomorrow实用解析
Sep 25 Python
Python 实现将大图切片成小图,将小图组合成大图的例子
Mar 14 Python
Python json转字典字符方法实例解析
Apr 13 Python
小白教你PyCharm从下载到安装再到科学使用PyCharm2020最新激活码
Sep 25 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
德生PL550的电路分析
2021/03/02 无线电
了解咖啡雨林联盟认证 什么是雨林认证 雨林认证是什么意思
2021/03/05 新手入门
PHP自定session保存路径及删除、注销与写入的方法
2014/11/18 PHP
php替换字符串中间字符为省略号的方法
2015/05/04 PHP
js直接编辑当前cookie的脚本
2008/09/14 Javascript
jQuery实现的类flash菜单效果代码
2010/05/17 Javascript
JavaScript中也使用$美元符号来代替document.getElementById
2010/06/19 Javascript
阻止事件(取消浏览器对事件的默认行为并阻止其传播)
2013/11/03 Javascript
使用console进行性能测试
2015/04/27 Javascript
JavaScript html5 canvas画布中删除一个块区域的方法
2016/01/26 Javascript
jQuery简单倒计时效果完整示例
2016/09/20 Javascript
Vue中如何实现轮播图的示例代码
2017/07/27 Javascript
解决vue-cli创建项目的loader问题
2018/03/13 Javascript
浅析vue 函数配置项watch及函数 $watch 源码分享
2018/11/22 Javascript
vue中axios实现数据交互与跨域问题
2019/05/12 Javascript
微信小程序发布新版本时自动提示用户更新的方法
2019/06/07 Javascript
layui layer select 选择被遮挡的解决方法
2019/09/21 Javascript
在vue中高德地图引入和轨迹的绘制的实现
2019/10/11 Javascript
浅析vue cli3 封装Svgicon组件正确姿势(推荐)
2020/04/27 Javascript
element-ui点击查看大图的方法示例
2020/12/14 Javascript
[14:24]Optic Gaming vs PSG LGD BO3
2018/06/07 DOTA
python中import学习备忘笔记
2017/01/24 Python
python2与python3共存问题的解决方法
2018/09/18 Python
python2与python3中关于对NaN类型数据的判断和转换方法
2018/10/30 Python
Django实现auth模块下的登录注册与注销功能
2019/10/10 Python
6行Python代码实现进度条效果(Progress、tqdm、alive-progress​​​​​​​和PySimpleGUI库)
2020/01/06 Python
Python运行DLL文件的方法
2020/01/17 Python
matplotlib.pyplot.plot()参数使用详解
2020/07/28 Python
详解matplotlib中pyplot和面向对象两种绘图模式之间的关系
2021/01/22 Python
浅谈cookie和localStorage那些事
2019/08/27 HTML / CSS
献爱心倡议书
2014/04/14 职场文书
高中生学习计划书
2014/09/15 职场文书
2014年前台接待工作总结
2014/12/05 职场文书
小学思品教学反思
2016/02/20 职场文书
八年级作文之一起的走过日子
2019/09/17 职场文书
css3 利用transform-origin 实现圆点分布在大圆上布局及旋转特效
2021/04/29 HTML / CSS