摘自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[k]存在,使用它。如果不存在增加一个新的D[k] .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 = ["cat", "bear", "cat", "dog"]

for word in words:
# Look up the function to call for each word, then call it
functionToCall = tokenDict[word]
functionToCall( )
# You could also do it in one step, tokenDict[word]( )

5.给定两个字典,你要找出它们共有的键的集合。如:print "Intersects:", filter(another_dict.has_key, some_dict.keys())
或print "Intersects:", [k for k in some_dict if k in another_dict] 这两种方法会比较好一些

6.你想在一个list上的所有元素上执行一个操作.但是你想避免使用map和filter .因为他们难以阅读和理解, 特别是当使用lambda的时候.
如:thenewlist = [x + 23 for x in theoldlist if x > 5]

7.你需要在一个序列上循环,但是在每一步,你也需要知道什么索引进入到你已经到达的序列中。
如:for index in range(len(sequence)):
something(sequence[index], index)
或:class Indexed:
def _ _init_ _(self, seq):
self.seq = seq
def _ _getitem_ _(self, i):
return self.seq[i], i

for item, index in Indexed(sequence):
something(item, index)

8.你需要变换一个列表的列表,把行转换成列,反之亦然。
如:print [[r[col] for r in arr] for col in range(len(arr[0]))]

9.你想建立一个多维list, 但是最简单的方法充满惊奇
如:multilist = [[0 for col in range(5)] for row in range(10)]

10.想排序一个字典。因为排序是一个仅仅对序列有意义的概念。
如:def sortedDictValues2(adict):
keys = adict.keys( )
keys.sort( )
return [adict[key] for key in keys]
更好的一种方法:
def sortedDictValues3(adict):
keys = adict.keys( )
keys.sort( )
return map(adict.get, keys)

11.需要有效的处理来自于两个大的相关数据集合的数据对。使用一个辅助的字典去做数据的预处理。因此会减少在大多数不相关数据上的迭代。
如(原方法):results = [ process(webhit, ccinfo) for webhit in weblog for ccinfo in cclog \
if ccinfo.ipaddress==webhit.ipaddress ]
新方法:
ipdict = {}
for webhit in weblog: ipdict.setdefault(webhit.ipaddress, []).append(webhit)
results = [ process(webhit, ccinfo) for ccinfo in cclog \
for webhit in ipdict[ccinfo.ipaddress] ]

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 [ item for item, index in decorated ]

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[item_insert_point-1:item_insert_point] == [theitem]
或:is_present = thelist and thelist[item_insert_point-1] == 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[index]
# direct deletion, no search needed
del data[index]
process(elem)


16.在一个序列里,你需要为成员关系执行频繁的测试。在操作上重复O(N)行为损害了性能。但是你不 能切换去使用一个字典,因为你需要这个序列的顺序。
如:def addUnique2(baseList, otherList):
auxDict = {}
for item in baseList:
auxDict[item] = None
for item in otherList:
if not auxDict.has_key(item):
baseList.append(item)
auxDict[item] = None

17.三行代码中展示Quicksort
def qsort(L):
if len(L) <= 1: return L
return qsort([lt for lt in L[1:] if lt < L[0]]) + L[0:1] + \
qsort([ge for ge in L[1:] if ge >= L[0]])


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 [c in str for c in set]

def containsAll(str, set):
""" Check whether sequence str contains ALL of the items in set. """
return 0 not in [c in str for c in set]

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值