1. 深拷贝与浅拷贝
- Python中对象的赋值都是进行对象引用(内存地址)传递
- 使用copy.copy(),可以进行对象的浅拷贝,它复制了对象,但对于对象中的元素,依然使用原始的引用.
- 如果需要复制一个容器对象,以及它里面的所有元素(包含元素的子元素),可以使用copy.deepcopy()进行深拷贝
- 对于非容器类型(如数字、字符串、和其他’原子’类型的对象)没有被拷贝一说
- 如果元祖变量只包含原子类型对象,则不能深拷贝。
2.使用Counter进行计数统计
"""
@python version: python3.6.1
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@site: http://blog.csdn.net/github_36326955
@software: PyCharm
@file: counter.py
@time: 5/7/2017 7:32 PM
"""
from collections import Counter
data = ['a', '2', 2, 4, 5, '2', 'b', 4, 7, 'a', 5, 'd', 'a', 'z']
print(Counter(data))
print(list(Counter(data).elements())) """Counter(data).elements()获取Counter中的key值,是一个chain的迭代器"""
print(Counter(data).most_common(2))
"""找出前n个出现频率最高的元素以及他们对应的次数"""
print(Counter(data)['y'])
"""访问不存在的元素时,返回0,而不是KeyError异常"""
"""
output:
Counter({'a': 3, '2': 2, 4: 2, 5: 2, 2: 1, 'b': 1, 7: 1, 'd': 1, 'z': 1})
['a', 'a', 'a', '2', '2', 2, 4, 4, 5, 5, 'b', 7, 'd', 'z']
[('a', 3), ('2', 2)]
0
"""
"""
update()用于被统计对象元素的更新,原有Count计数器对象与新增元素的统计计数值相加。
subtract()用于实现计数器对象中统计值相减,允许有负值。
"""
c = Counter("success")
print(c)
c.update("successfully")
print(c)
c.subtract("successzz")
print(c)
"""
output:
Counter({'s': 3, 'c': 2, 'u': 1, 'e': 1})
Counter({'s': 6, 'c': 4, 'u': 3, 'e': 2, 'l': 2, 'f': 1, 'y': 1})
Counter({'s': 3, 'u': 2, 'c': 2, 'l': 2, 'e': 1, 'f': 1, 'y': 1, 'z': -2})
"""
a = Counter(a=1,b=2,c=3,d=1)
print(a)
3.