python集合用法实例分析

 更新时间:2015年05月30日 11:04:54   作者:不吃皮蛋  
这篇文章主要介绍了python集合用法,较为详细的分析了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)
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程序设计有所帮助。

相关文章

  • 详解爬虫被封的问题

    详解爬虫被封的问题

    这篇文章主要介绍了爬虫被封的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Python+Pygame实现简单的射击小游戏

    Python+Pygame实现简单的射击小游戏

    要说什么游戏能够获得大家的喜爱,唯射击游戏莫属!本文将利用Python和Pygame库制作一个简单的射击小游戏,感兴趣的小伙伴可以了解一下
    2022-04-04
  • 跟老齐学Python之大话题小函数(2)

    跟老齐学Python之大话题小函数(2)

    上篇文章我们讲诉了map 和lambda函数的使用,本文我们继续来看看reduce和filter函数,有需要的朋友可以参考下
    2014-10-10
  • python实现进度条和系统通知的示例详解

    python实现进度条和系统通知的示例详解

    这篇文章主要和大家分享两个有意思的Python小工具,可以优雅地实现进度条和系统通知,文中的示例代码简洁易懂,有需要的小伙伴快也跟随小编一起学习一下
    2023-11-11
  • python自动化测试工具Helium使用示例

    python自动化测试工具Helium使用示例

    大家好,本篇文章主要讲的是python自动化测试工具Helium使用示例,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下哦
    2021-12-12
  • Django 配置多站点多域名的实现步骤

    Django 配置多站点多域名的实现步骤

    这篇文章主要介绍了Django 配置多站点多域名的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Python实现免费音乐下载器

    Python实现免费音乐下载器

    本文主要为大家介绍了通过Python实现的免费音乐下载器,文中的示例代码讲解详细,对我们的学习或工作有一定的帮助,需要的小伙伴可以学习一下
    2021-12-12
  • 浅谈dataframe中更改列属性的方法

    浅谈dataframe中更改列属性的方法

    今天小编就为大家分享一篇浅谈dataframe中更改列属性的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Python进阶学习之你真的懂元组吗?

    Python进阶学习之你真的懂元组吗?

    在我们学习python的过程中,对元组的介绍通常是成为”不可变的列表“,但是这其实并没有完全的概括元组的功能。在本文中,我们将会介绍元组作为记录的功能,话不多说我们开始吧
    2023-04-04
  • Python搭建HTTP服务器和FTP服务器

    Python搭建HTTP服务器和FTP服务器

    这篇文章主要为大家详细介绍了Python搭建HTTP服务器和FTP服务器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03

最新评论