摘自python cookbook1(字符串,字典)
摘自python cookbook1(字符串,字典) url:http://wiki.woodpecker.org.cn/moin/PyCookbook
1.变量互换值,在java里需要使用临时变量,在python中则可以不必这样,只需 a,b,c=c,a,b这样的即可
2.你需要从字典中获得一个值,不要处理在字典里找不到你所需要的键值的异常。dic.get('key', 'not found')
3.用一个字典D工作,如果D存在,使用它。如果不存在增加一个新的D .d.setdefault(word, ).append(pagenumber)
4.你需要执行一段合适的代码去和一些控制变量的值来通信。问题在于在一些其他语言里,你可能要 处理case,switch, 或者select语句。 在python里可以使用字典来进行处理。如下所示: animals = number_of_felines = 0
def deal_with_a_cat( ):
global number_of_felines
print "meow"
animals.append('feline')
number_of_felines += 1
def deal_with_a_dog( ):
print "bark"
animals.append('canine')
def deal_with_a_bear( ):
print "watch out for the *HUG*!"
animals.append('ursine')
tokenDict = {
"cat": deal_with_a_cat,
"dog": deal_with_a_dog,
"bear": deal_with_a_bear,
}
# Simulate, say, some words read from a file
words =
for word in words:
# Look up the function to call for each word, then call it
functionToCall = tokenDict
functionToCall( )
# You could also do it in one step, tokenDict( )
5.给定两个字典,你要找出它们共有的键的集合。如:print "Intersects:", filter(another_dict.has_key, some_dict.keys()) 或print "Intersects:", 这两种方法会比较好一些
6.你想在一个list上的所有元素上执行一个操作.但是你想避免使用map和filter .因为他们难以阅读和理解, 特别是当使用lambda的时候. 如:thenewlist =
7.你需要在一个序列上循环,但是在每一步,你也需要知道什么索引进入到你已经到达的序列中。 如:for index in range(len(sequence)): something(sequence, index) 或:class Indexed: def _ init _(self, seq): self.seq = seq def _ getitem _(self, i): return self.seq*, i
for item, index in Indexed(sequence): something(item, index)
8.你需要变换一个列表的列表,把行转换成列,反之亦然。 如:print for r in arr] for col in range(len(arr))]
9.你想建立一个多维list, 但是最简单的方法充满惊奇 如:multilist = for row in range(10)]
10.想排序一个字典。因为排序是一个仅仅对序列有意义的概念。 如:def sortedDictValues2(adict): keys = adict.keys( ) keys.sort( ) return for key in keys] 更好的一种方法: def sortedDictValues3(adict): keys = adict.keys( ) keys.sort( ) return map(adict.get, keys)
11.需要有效的处理来自于两个大的相关数据集合的数据对。使用一个辅助的字典去做数据的预处理。因此会减少在大多数不相关数据上的迭代。 如(原方法):results = 新方法: ipdict = {} for webhit in weblog: ipdict.setdefault(webhit.ipaddress, ).append(webhit) results = ]
12.你需要一个保证稳定的方法来排序一个python list。除了速度之外, 装饰-排序-去除装饰(DSU)提供了比仅仅使有一个比较函数参数的排序所没有的弹性。 如:def stable_sorted_copy(alist, _indices=xrange(sys.maxint)): # Decorate: prepare a suitable auxiliary list decorated = zip(alist, _indices)
# Sort: do a plain built-in sort on the auxiliary list
decorated.sort( )
# Undecorate: extract the list from the sorted auxiliary list
return
def stable_sort_inplace(alist): # To sort in place: assign sorted result to all-list slice of original list alist = stable_sorted_copy(alist)
13.二分查找法是一个在python通过bisect来提供的基本算法。怎样去执行一个二分查找去检查是否一个值出现在一个list里可以总结成三行代码 如:thelist.sort( ) item_insert_point = bisect.bisect(thelist, theitem) is_present = thelist == 或:is_present = thelist and thelist == theitem
14.你有一个对象list,你需要根据每个对象的一个属性来对他们排序,要尽可能的快和方便。 如:def sort_by_attr(seq, attr): import operator intermed = map(None, map(getattr, seq, (attr,)*len(seq)), xrange(len(seq)), seq) intermed.sort( ) return map(operator.getitem, intermed, (-1,)*len(intermed))
def sort_by_attr_inplace(lst, attr):
lst = sort_by_attr(lst, attr)
15.不用循环从一个list中选择随机的元素 如: def best( ): random.shuffle(data) for elem in data: process(elem)
# or, if you need to preserve the data list's original ordering:
def best_preserve( ):
aux = list(data)
random.shuffle(aux)
for elem in aux: process(elem)
或:def faster( ): while data: index = random.randrange(len(data)) elem = data # direct deletion, no search needed del data process(elem)
16.在一个序列里,你需要为成员关系执行频繁的测试。在操作上重复O(N)行为损害了性能。但是你不 能切换去使用一个字典,因为你需要这个序列的顺序。 如:def addUnique2(baseList, otherList): auxDict = {} for item in baseList: auxDict = None for item in otherList: if not auxDict.has_key(item): baseList.append(item) auxDict = None
17.三行代码中展示Quicksort
def qsort(L):
if len(L) <= 1: return L
return qsort( if lt < L]) + L +
qsort( if ge >= L])
18.判断是否是字符串 如:def isStringLike(anobj): try: anobj + '' except: return 0 else: return 1
19.有几个小的字符串,你想吧他们合并成一个大的字符串 如:为了把短的片断放在少数的变量中,字符串操作符'%'经常是更好的选择! largeString = '%s%s something %s yet more' % (small1, small2, small3) 或:为了加入一系列的短字符串到一个大的字符串中,字符串操作符join总是最好的: largeString = ''.join(pieces)
20.你需要检查检查是否一个集合的字符在某个字符串中出现. 如:def containsAny(str, set): """ Check whether sequence str contains ANY of the items in set. """ return 1 in
def containsAll(str, set): """ Check whether sequence str contains ALL of the items in set. """ return 0 not in
21.测一个字符串是否表达一个整数 如:def isInt(astring): """ Is the given string an integer? """ try: int(astring) except ValueError: return 0 else: return 1 或:def isAllDigits(astring): """ Is the given string composed entirely of digits? """ # In Python 2.0 and later, "astring.isdigit( ) or not astring" is faster import string acceptable_characters = string.digits for acharacter in astring: if acharacter not in acceptable_characters: return 0 return 1