#python Cookbook 2nd edition study (all recorded here)

Source: Internet
Author: User


#标题: Build lists with List resolution
#例1: For example, to add a number to each subkey in the list, as follows:
def myList (L):
return ([x+20 for X in L])
Print MyList ([1,2,3,4,5,6,7,8])
#例2: For example, to find a list larger than a certain number to build a new list, as follows
def myList (L):
return ([x for x in L if X>5])
Print MyList ([1,2,3,4,5,6,7,8])
#对例1和例2进行合并, as follows:
def myList (L):
return ([x+20 for x in L if X>5])
Print MyList ([1,2,3,4,5,6,7,8])
#说明: List resolution is the best way to consider changing a list, rather than creating a new one, such as the need to set a list L greater than 100 to 100, as follows
def myList1 (L):
L[:]=[min (x,100) for x in L]
Print L
MyList1 (list (range (90,102)))
#注意: It is not recommended to use list parsing when executing a loop!
#比如要复制一个列表, write directly to list (L), and when a function is called on each element, use map (f,l)
#例3: Summing a list subkey
def myList (L):
return sum ([X+20for x in L if X>5])
Print MyList (range (20))
#或者写成如下, without parentheses
def myList (L):
return sum (X+20for x in L if x>5)
Print myList (range) #这样做的原因是可以一次性将整个列表载入到内存

#给字典增加一个条目
#标题: Given a dictionary d, when K is the key to the dictionary, directly using d[k], if K is not the D key, then create this new entry, as follows
def addwork (index,word,page):
Index.setdefault (word,[]). Append (page)
#反转字典
#给定一个字典, this dictionary maps different keys to different values, and you want to create a reversed dictionary that reflects each value to the key
#通过dict列表解析式解决这个问题
def invert_dict (d):
Return Dict ([(v,k) for k,v in D.iteritems ()])

Print invert_dict ({1:2, ' a ': 4})
#对于比较大的字典, you can use the Izip () of the Itertools module
From Itertools import Izip
def invert_dict (d):
Return Dict (Izip (D.iteritems (), D.iterkeys ()))
Print invert_dict ({1:2, ' a ': 4})
"""
If the value in dictionary D is not unique, then D cannot be really reversed, i.e. there is no such dictionary, for any given key K, satisfies
Id[d[k]]==k, but for any of the dictionary D-value v,d[pd[v]]=v, given your original dictionary d, you can check if x is a D inverse dictionary or a pseudo-inverse dictionary: When and only Len (x) ==len (d), X is the true inverse Dictionary of D
Because if 2 different keys correspond to the same value, one of the 2 keys will definitely disappear for the 2 functions given by the solution.
Thus the generated pseudo-inverse dictionary length will be shorter than the original dictionary length, in any case, only if the value in D is hashed, then the previous function will work properly, otherwise the function would give a TypeError exception
This function obtains the Iteritems method to generate a pair of keys and other corresponding values K and V, wrapping them into (v,k) Order
and assigns the final generation sequence as a parameter to the dict, so that dict constructs a key for a value, and the original key becomes a new dictionary of the corresponding value,
"""

#python Cookbook 2nd edition study (all recorded here)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.