python集合用法实例分析
作者:不吃皮蛋 发布时间:2021-10-26 23:36:37
标签:python,集合
本文实例讲述了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)
# 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程序设计有所帮助。
0
投稿
猜你喜欢
- 一个可能你似曾相识的场景阅读内容包含大量英文的 PPT、Word、Excel 或者记事本时,由于英语不熟悉,为了流利地阅读,需要打开浏览器进
- 一些大型站点都有着非常一流的视觉外观,它们很可能在设计时使用了网格系统。网格可以是页面布局显得紧凑而且稳定,为设计师在设计站点时提供一个逻辑
- Python实战系列用于记录实战项目中的思路,代码实现,出现的问题与解决方案以及可行的改进方向本文为第2篇–200行Python代码实现20
- 代码:import tensorflow as tfsess = tf.Session()check_point_path = 'v
- 1.外形尺寸尺寸单位:只用默认的像素或者其他字符类的值!,不要用英寸毫米之类的内容。btn = tkinter.Button(root,te
- 引用是指保存的值为对象的地址。在 Python 语言中,一个变量保存的值除了基本类型保存的是值外,其它都是引用,因此对于它们的使用就需要小心
- 概述从今天开始, 小白我将带领大家一起来补充一下 数据库的知识.自连接自连接 (Self Join) 是一种特殊的表连接. 自连接指相互连接
- asp如何将RGB颜色转化成到16进制的?在R G B中输入小于255的数字点击观看即可转换成#开通的16进制。代码如下:<%R_RG
- 程序能实现什么a.完成gap值的自定义输入以及两条需比对序列的输入b.完成得分矩阵的计算及输出c.输出序列比对结果d.使用matplotli
- <% Response.Buffer = True Server.ScriptTimeOut=9999999&
- 目录一、图片处理(一)图片采集(二)图片装载(三)完整代码二、初始化pygame相关参数(一)设置初始化参数(二)设置为全屏显示:三、核心模
- Player.playState0 Undefined Windows Media Player is in an undefined st
- Python写一些简单的GUI界面也是非常简单的,并且Python有着丰富的库,这些库可以很方便我们去操作Windows系统,搭配界面,可以
- 1、执行环境说明python版本3.7直接使用pip进行安装pywin32、pyinstallerpip install pywin32pi
- 1、模拟退火算法退火是金属从熔融状态缓慢冷却、最终达到能量最低的平衡态的过程。模拟退火算法基于优化问题求解过程与金属退火过程的相似性,以优化
- 需求:连接本机的(两台安卓手机)或者本机安装的(安卓模拟器两个),实现同时安装本地apk包 。demon.py特别说明:必须写udid才能实
- 前言总结一下最近看的关于opencv图像几何变换的一些笔记. 这是原图: 1.平移import cv2import numpy as npi
- 本文实例为大家分享了python实现决策树分类算法的具体代码,供大家参考,具体内容如下1、概述决策树(decision tree)——是一种
- 首先将ORACLE 10g的安装光盘放入光驱,如果自动运行,一般会出现如图1安装界面: 图1 单击“开始安装”,就可以安装ORACLE 10
- 引言:Flask是Python社区非常流行的一个Web开发框架,本文将尝试将介绍APScheduler应用于Flask之中。1. Flask