`
canofy
  • 浏览: 832414 次
  • 性别: Icon_minigender_1
  • 来自: 北京、四川
社区版块
存档分类
最新评论

摘自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
分享到:
评论

相关推荐

    PythonCookbook3高清.pdf

    1. Python 基础知识:该书首先介绍了 Python 基础知识,包括 Python 语法、变量、数据类型、控制结构、函数、模块等内容。 2. 数据结构:该书详细介绍了 Python 中的数据结构,包括列表、元组、字典、集合等数据...

    Python3 Cookbook:《Python CookBook》一直是较为经典的Python教程

    《Python CookBook》一直是较为经典的Python教程。它注重方法和技巧的讲解,能让学习者更好的理解Python这门语言,最终将技巧运用到项目中。本书作者是David Beazley大神,一位独立的计算机科学家、教育家,以及有着...

    Python Cookbook 第3版 中文版 pdf

    Python Cookbook 第3版 中文版 Python Cookbook 第3版 中文版

    Python Cookbook 英文版.pdf

    根据提供的文件信息,内容来自于《Python Cookbook》第三版,这本书是由David Beazley和Brian K. Jones共同编著的,由O’Reilly Media, Inc.出版。在介绍这本书时,我们要关注Python编程中的数据结构和算法的应用。 ...

    PythonCookbook.zip

    《Python Cookbook》提供了关于正则表达式、字符串格式化、文本分词和编码转换的技巧。 6. **面向对象编程**:Python是面向对象的语言,书中涵盖了类的定义、继承、多态,以及如何利用元类(metaclass)进行高级...

    python cookbook 第三版

    ### Python Cookbook 第三版知识点概览 #### 一、书籍简介 《Python Cookbook》第三版是一本面向Python进阶学习者的实用指南。本书由熊能编译,发布于2016年1月21日,版本号为1.0.2。本书适合已经具备一定Python...

    Python Cookbook(第3版)中文版.pdf 极清PDF

    Python Cookbook(第3版)中文版.pdf 极清PDF

    python cookbook 英文版

    - 使用`eval()`函数可以执行存储在字符串中的Python表达式,但需注意安全性问题,避免执行不可信来源的代码。 以上只是《Python Cookbook》英文版中涵盖的部分知识点,这本书深入浅出地讲解了Python的各种使用...

    Python Cookbook_python_

    《Python Cookbook》是一本深受Python程序员喜爱的实战指南,它由David Beazley和Brian K. Jones合著,是Python编程领域中的经典之作。这本书旨在帮助开发者解决在实际编程过程中遇到的各种问题,提供了大量实用的...

    Python Cookbook

    1. **3.1 逐字符处理字符串**:介绍了如何使用循环来逐字符地处理字符串。 2. **3.2 测试对象是否类似字符串**:提供了检查对象是否具有字符串行为的方法。 3. **3.3 对齐字符串**:讲解了如何使用字符串格式化或...

    python cookbook 中文版

    书中提供了很多关于编写清晰、简洁代码的建议,包括如何有效地使用文档字符串、异常处理、单元测试以及代码重构。通过这些实践,你可以提高代码的可维护性,降低未来修改和扩展的成本。 总的来说,这本中文版的...

    Python CookBook随书代码

    7. **字符串与正则表达式**:Python的字符串操作强大,而正则表达式提供了一种匹配和操作复杂文本模式的方法。 8. **数据结构优化**:书中可能会讨论如何有效地使用`set`进行集合操作,以及如何利用`dict`的哈希...

    Python Cookbook, 2nd Edition

    Python Cookbook, 2nd Edition, Python Cookbook, 2nd Edition, Python Cookbook, 2nd Edition

    python cookbook 第三版中文版

    《Python Cookbook》第三版中文版是一本详细介绍Python编程实践技巧的书籍。这本书适合各个层次的Python程序员阅读,尤其适合那些希望通过实际案例学习Python编程的读者。书中不仅包含了丰富的代码示例,还有大量...

    《Python Cookbook》第三版中文v2.0.0.pdf

    3. **字符串和正则表达式**:Python在处理文本数据方面非常强大,Cookbook详细讲解了字符串操作和正则表达式的高级用法,如模式匹配、替换、分割和提取信息。 4. **迭代器和生成器**:Python的迭代器和生成器机制...

    PythonCookbook3rd.pdf

    8. **字符串和文本处理**:详细介绍了Python的字符串操作,如正则表达式、字符串格式化以及Unicode处理,这些都是处理文本数据的关键。 9. **网络编程**:涵盖网络通信的基础,如套接字编程,以及如何使用requests...

    python cookbook_python编程资料_

    3. **字符串和文本处理**:字符串在Python中是常用的数据类型,书中会讲解字符串的拼接、格式化输出、正则表达式匹配等。此外,还会有章节涉及文本处理,如读写文件、XML和JSON解析等。 4. **错误和异常处理**:...

Global site tag (gtag.js) - Google Analytics