• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python集合用法实例分析

python集合用法实例分析

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python脚本编写实例,python项目实例,python开发实例,python开发网站实例,python web开发实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python脚本爬取字体文件的实现方法
  • Python只用40行代码编写的计算器实例
  • Python实现脚本锁功能(同时只能执行一个脚本)
  • python脚本爬取字体文件的实现方法
  • python获取当前运行函数名称的方法实例代码
  • Python类的动态修改的实例方法
  • Python中动态创建类实例的方法
  • Python自动发邮件脚本
  • Python类的动态修改的实例方法
  • Python中动态创建类实例的方法

相关文章

  • Python中的模块导入和读取键盘输入的方法
  • Python基于Tkinter的HelloWorld入门实例
  • 使用Python编写vim插件的简单示例
  • Python实现多行注释的另类方法
  • python 循环遍历字典元素的简单方法
  • Python中的装饰器用法详解
  • python实现折半查找和归并排序算法
  • python获取指定网页上所有超链接的方法
  • Python中的匿名函数使用简介
  • Python中的with语句与上下文管理器学习总结

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 使用Python的Twisted框架实现一个简单的服务器
    • 用Python抢过年的火车票附源码
    • python实现二维码扫码自动登录淘宝
    • 下载安装setuptool和pip linux安装pip
    • python网络编程之数据传输UDP实例分析
    • 关于Python中Inf与Nan的判断问题详解
    • 在Python的Flask中使用WTForms表单框架的基础教程
    • 深入解读Python解析XML的几种方式
    • SQLite3中文编码 Python的实现
    • python简单实现旋转图片的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有