Python入门篇-Python集、字典

              Python入门篇-Python集合(set)、字典(dict)

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.集合(set)

1>.集合的特点

约定
  set 翻译为集合
  collection 翻译为集合类型,是一个大概念

set   可变的、无序的、不重复的元素的集合

2>.set定义和初始化

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

'''
  set() -> new empty set object
  set(iterable) -> new set object
'''

s1 = set()

s2 = set(range(10))

s3 = set(list(range(20)))

#dict
s4 = {}

#set
s5 = {1,3,5,7,9}

s6 = {(1,3),5,'A'}

#集合只能存放不可变的的元素,如果存放list和bytearray时会报错:"unhashable type"
s7 = {(2,),3,None,"abc",b"ABC"}

s8 = set(enumerate(range(5)))

print(s1)
print(s2)
print(s3)
print(s4)
print(s5)
print(s6)
print(s7)
print(s8)



#以上代码执行结果如下:
set()
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
{}
{1, 3, 5, 7, 9}
{'A', (1, 3), 5}
{3, None, (2,), b'ABC', 'abc'}
{(0, 0), (3, 3), (4, 4), (2, 2), (1, 1)}

3>.set的元素

set的元素要求必须可以hash

目前学过的不可hash的类型有list、set

元素不可以索引

set可以迭代

4>.set增加

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


'''
add(elem)
    增加一个元素到set中
    如果元素存在,什么都不做
'''

s1 = {1,3,5}
print(s1)

s1.add(100)
print(s1)



#以上代码执行结果如下:
{1, 3, 5}
{1, 3, 100, 5}
add(elem) 增加一个元素到set中,如果元素存在,什么都不做
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


'''
update(*others)
    合并其他元素到set集合中来
    参数others必须是可迭代对象
    就地修改
'''

s1 = {1,3,5}
print(s1,id(s1))

s1.update([1,3,2],[2,3,4],(6,8))
print(s1,id(s1))



#以上代码执行结果如下:
{1, 3, 5} 31487144
{1, 2, 3, 4, 5, 6, 8} 31487144
update(*others) 合并其他元素到set集合中来,参数others必须是可迭代对象,就地修改

5>.set删除

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


'''
remove(elem)
    从set中移除一个元素
    元素不存在,抛出KeyError异常。为什么是KeyError?因为这个key对应的是一个hash值。
'''

s1 = {1,3,5}
print(s1)

s1.remove(3)
print(s1)



#以上代码执行结果如下:
{1, 3, 5}
{1, 5}
remove(elem) 从set中移除一个元素,元素不存在,抛出KeyError异常
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


'''
pop() -> item
    移除并返回任意的元素。为什么是任意元素?
    空集返回KeyError异常
'''

s1 = {1,3,5}
print(s1)

s1.pop()
print(s1)



#以上代码执行结果如下:
{1, 3, 5}
{3, 5}
pop() -> item 移除并返回任意的元素。为什么是任意元素? 空集返回KeyError异常
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


'''
discard(elem)
     从set中移除一个元素
    元素不存在,什么都不做
'''

s1 = {1,3,5}
print(s1)

s1.discard(3)
print(s1)



#以上代码执行结果如下:
{1, 3, 5}
{1, 5}
discard(elem) 从set中移除一个元素,元素不存在,什么都不做
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


'''
clear()
    移除所有元素
'''

s1 = {1,3,5}
print(s1)

s1.clear()
print(s1)



#以上代码执行结果如下:
{1, 3, 5}
set()
clear() 移除所有元素

6>.set修改、查询

修改
  要么删除,要么加入新的元素
  为什么没有修改?

查询   非线性结构,无法索引
遍历   可以迭代所有元素
成员运算符   
in 和not in 判断元素是否在set中   效率要比线性结构数据要高,时间复杂度为O(1)。

7>.set成员运算符的比较

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

import datetime

#注意,这个值我们可以调大里面的数字进行测试,如果在后面再加一个0就会把内存吃满。
list_1 = list(range(100000000))
start = datetime.datetime.now()
a = -1 in list_1
end = datetime.datetime.now()

print("在列表中遍历一个数字耗时为:{}".format(end - start))

set1 = set(range(100000000))
start = datetime.datetime.now()
a = -1 in set1
end = datetime.datetime.now()
print("在集合中遍历一个数字耗时为:{}".format(end - start))



#以上代码执行结果如下:
在列表中遍历一个数字耗时为:0:00:01.236070
在集合中遍历一个数字耗时为:0:00:00

8>.set和线性结构

  线性结构的查询时间复杂度是O(n),即随着数据规模的增大而增加耗时
  set、dict等结构,内部使用hash值作为key,时间复杂度可以做到O(1),查询时间和数据规模无关
  
  可hash     数值型int、float、complex     布尔型True、False     字符串string、bytes     tuple     None     以上都是不可变类型,成为可哈希类型,hashable
  set的元素必须是可hash的

9>.

10>.

11>.

12>.

13>.

14>.

15>.

二.字典(dict)

猜你喜欢

转载自www.cnblogs.com/yinzhengjie/p/10897485.html