Python's * expression. It is necessary to break out n elements from an iterative object, but the length of the iteration object may be more than N, which results in an exception of "too many values to explode (too many values to unpack)".
For example:
Record = {' Dave ', ' [email protected] ', ' 773-555-1212 ', ' 847-555-1212 '}
Name,email,*phone_numbers = User_record
>>>phone_numbers
[' 773-555-1212 ', ' 847-555-1212 ']
Sometimes you might want to break out some values and discard them. When decomposing, you cannot just specify a single *, but you can use several commonly used variable names that represent the values to be discarded, such as: _ or IGN (ignored)
For example:
Record = (' ACME ', 50,123.45, (12,18,2012))
Name,*_, (*_,year) = record
>>>year
2012
Find the largest or smallest n elements
There are two functions in the HEAPQ module---nlargest () and Nsmallest ()
Import HEAPQ
Nums ={1,8,2,23,7,-4,18,23,42,37,2}
Print (Heapq.nlargest (3,nums)) [42,37,23] # Get the largest three numbers
Print (Heapq.nsmallest (3,nums)) [ -4,1,2] #得到嘴笑的三个数字
This article from "11565952" blog, declined reprint!
Data Analysis Learning record one