设为首页 收藏本站
查看: 593|回复: 0

[经验分享] 摘自python cookbook1(字符串,字典)

[复制链接]

尚未签到

发表于 2017-5-2 10:23:38 | 显示全部楼层 |阅读模式
摘自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

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-371997-1-1.html 上篇帖子: gearman的安装启动以及python API入门例子 下篇帖子: Learning.Python.3rd.Edition 笔记[27-29]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表