1. Deep copy and shallow copyObjects in Python are assigned to object reference (memory address) delivery using copy.copy (), you can make a shallow copy of the object, it duplicates the object, but for the elements in the object, still use the original reference. If you need to copy a container object, and all of the elements inside it (including the element's child elements), you can use Copy.deepcopy () to make a deep copy of a non-container type (such as numbers, strings, and other ' atomic ' types of objects) without being copied. If the meta ancestor variable contains only atomic type objects, You cannot copy deep.
2. Use counter for Counting statistics
#!/usr/bin/env python # encoding:utf-8 "" "@python version:python3.6.1 @author: Xiangguosun @contact: Sunxiangguodut@q Q.com @site: http://blog.csdn.net/github_36326955 @software: pycharm @file: counter.py @time: 5/7/2017 7:32 PM "" "from Co Llections Import Counter data = [' A ', ' 2 ', 2, 4, 5, ' 2 ', ' B ', 4, 7, ' A ', 5, ' d ', ' a ', ' Z '] print (Counter (data)) print (LIS T (Counter (data). Elements ()) "" "Counter (data). Elements () Gets the key value in Counter, which is a chain iterator" "" Print Counter (data). Most
_common (2)) "" "" "" "" to find the first n most frequently occurring elements and their corresponding number of times "" Print (Counter (data) [' Y ']) "" "to access the element that does not exist, returns 0 instead of Keyerror exception" "" "" Output: Counter ({' A ': 3, ' 2 ': 2, 4:2, 5:2, 2:1, ' B ': 1, 7:1, ' d ': 1, ' Z ': 1} ' [' A ', ' a ', ' a ', ' a ', ' a ', ' 2 ', ' 2 ', 2, 4, 4, 5, 5, ' B ', 7,
' d ', ' Z '] [(' A ', 3], (' 2 ', 2)] 0 "" "" "" Update () is used to update the element of the statistic object, and the original Count counter object is added to the statistic count of the new element.
Subtract () is used to subtract statistical values from counter objects, allowing negative values. "" "C = Counter (" Success ") print (c) c.update (" successfully ") print (c) c.subtract (" Successzz ") print (c)" "" Output:counte R ({' 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, ' F ': 1 , ' Y ': 1, ' Z ':-2}] "" A = Counter (a=1,b=2,c=3,d=1) print (a) # output Counter ({' C ': 3, ' B ': 2, ' a ': 1, ' d ': 1}) 3.