Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1743395
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2016-04-26 11:52:17

set is an unordered collection of elements,is a map in which keys do not have associated values. 
python have builtins frozenset and set.frozenset is an immutable form. these are implemented using hash table.

点击(此处)折叠或打开

  1. from collections import MutableSet                #set
  2. from collections import Set                        #immutable set
基本操作:

点击(此处)折叠或打开

  1. some_set={7,2,3,5}
  2. some_set.add(6)                #add the item
  3. some_set.update({10,9})        #add new set to this set
  4. some_set.remove(6)            #remove if the item exist, or raise an KeyError Exception.
  5. some_set.discard(6)            #discard an item and no exception if it does not exist.
  6. some_set.pop()                #return an arbitrary item, raise KeyError if set is empty.
  7. some_set.clear()            #清空一个集合

点击(此处)折叠或打开

  1. sorted(some_set)
  2. max(some_set)
  3. min(some_set)
  4. sum(some_set)
  5. words=set('How I wish'.split())
  6. more=set('I would recollect pi'.split())    
  7. words|more                #words.union(more)                 并集
  8. words&more                #words.intersection(more)        交集
  9. words|=more                #words.update(more)                把第二个集合中不在第一个集合中的数据加入到第一个集合
  10. words-more                #words.difference(more)            第一个集合减去两个集合的交集
  11. words^more                #words.symmetric_difference(more)    剪去两个集合的交集,并把第二个集合中特有的元素加入第一个集合
  12. {1,2,3}.issubset({1,2,3,4,5,6})        #第一个集合是第二个集合的子集
  13. {1,2,3,4,5}.issubset({1,2,3})        #False
  14. {1,2,3}.issuperset({1,2,3,4,5,6})    #False
  15. {1,2,3,4,5,6}.issuperset({1,2,3})    #第一个集合是第二个集合的超集
  16. not({1,2,3}-{1,2,3,4,5})            #True,空集合为False
  17. not({1,2,3,4,5}-{1,2,3})            #False

  18. 'I' in words            #True
  19. {'I'} < words            #True
  20. {'How','I','wish'} <= words        #True
  21. S|T: | 是用__or__实现的
  22. S|=T:是__ior__ 实现
  23. 表示空集合用 set() ,最好不用{}



阅读(1952) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

shuangbaby1232016-06-02 15:41:13

OneAPM 是唯一的一个可以帮助你查看 Python 应用程序方方面面的工具。无论是从终端的用户体验还是服务器监控,OneAPM 支持追踪各种问题,例如:数据库查询缓慢、第三方 API 接口和 Web 服务器甚至数据缓存层缓慢等。可以在生产环境下监控你的应用。快来官网注册体验吧~