Python Study Notes (12) and python Study Notes

Source: Internet
Author: User

Python Study Notes (12) and python Study Notes

I. collections

1. namedtuple

Namedtuple is a function used to create a custom tuple object and specify the number of tuple elements. You can use attributes instead of indexes to reference an element of tuple.

from collections import namedtuplePoint = namedtuple('Point', ['x', 'y'])p = Point(1, 2)print p.xprint p.y

2. deque

Deque is a bidirectional list for efficient insert and delete operations. It is suitable for queue and stack operations.

From collections import dequeq = deque (['A', 'B', 'C']) q. append ('x') # Add q to the end. appendleft ('y') # Add print q # deque (['y', 'A', 'B', 'C', 'C']) to the header. pop () # pop-up element q. popleft () print q # deque (['A', 'B', 'C'])

3. defaultdict

When using dict, if the referenced key does not exist, a KeyError is thrown. If you want to return a default value when the Key does not exist, you can use defaultdict.

Note: Keys of OrderedDict are sorted by insert order, not by Key.

From collections import defadicdictdd = defaultdict (lambda: 'n'/A') dd ['key1'] = 'abc' print dd ['key1'] # key1 exists, return 'abc' print dd ['key2'] # If key2 does not exist, return the default value 'n'/'
4. OrderedDict

When using dict, keys are unordered. When performing iteration on dict, we cannot determine the order of keys. To maintain the order of keys, use OrderedDict:

From collections import OrderedDictd = dict ([('A', 1), ('B', 2), ('C', 3)]) print d # the keys of dict are unordered {'A': 1, 'C': 3, 'B': 2} od = OrderedDict ([('A ', 1), ('B', 2), ('C', 3)]) print od # The Key of OrderedDict is ordered OrderedDict ([('A', 1 ), ('B', 2), ('C', 3)])

5. Counter

Counter is a simple Counter. In fact, it is also a subclass of dict:

from collections import Counterc = Counter()for ch in 'programming':    c[ch] = c[ch] + 1    print c# Counter({'g': 2, 'm': 2, 'r': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'p': 1})

Ii. base64

Base64 is an arbitrary Binary-to-text string encoding method. It is often used to transmit a small amount of binary data in URLs, cookies, and webpages.

import base64print base64.b64encode('binary string')# 'YmluYXJ5AHN0cmluZw=='print base64.b64decode('YmluYXJ5IHN0cmluZw==')# 'binary string'

Iii. struct

Struct can be used to convert str and other binary data types.

import structprint struct.pack('>I', 10240099)  # '\x00\x9c@c'print struct.unpack('>IH', '\xf0\xf0\xf0\xf0\x80\x80') # (4042322160, 32896)

Iv. hashlib

Hashlib provides common digest algorithms such as MD5 and SHA1.

Digest algorithms are also called hash algorithms and hash algorithms. It converts data of any length into a fixed-length data string through a function.

import hashlibmd5 = hashlib.md5()md5.update('how to use md5 in python hashlib?')print md5.hexdigest()sha1 = hashlib.sha1()sha1.update('how to use sha1 in python hashlib?')print sha1.hexdigest()
MD5 is usually used to store the digest of user passwords. When a user logs on, the MD5 of the plaintext password entered by the user is calculated first, and then compared with the MD5 of the database storage. If the MD5 is consistent, the password is entered correctly; if they are inconsistent, the password is definitely incorrect.

5. itertools

1. itertools provides several infinite iterators:

Import itertoolsnatuals = itertools. count (1) for n in natuals: print n # print the natural number infinitely import itertoolscs = itertools. cycle ('abc') # note that A string is also A type of sequence for c in cs: print c # print out A, B, Cns = itertools infinitely. repeat ('A', 10) for n in ns: print n # print 10 times
Although infinite sequences can be iterated infinitely, we usually use the takewhile function to extract a finite Sequence Based on conditions:
import itertoolsnatuals = itertools.count(1)ns = itertools.takewhile(lambda x: x <= 10, natuals)for n in ns:    print n

2. chain can concatenate a group of iteration objects to form a larger iterator:

Import itertoolsfor c in chain ('abc', 'xyz'): print c # iteration effect: 'A' B ''C' 'X' 'y' Z'
3. groupby pick out the adjacent repeated elements in the iterator and put them together:
Import itertoolsfor key, group in itertools. groupby ('aaabbbccaa'): print key, list (group) ##################### A ['A', 'A ', 'A'] # B ['B', 'B', 'B'] # C ['C', 'C'] # A ['A ', 'A', 'a'] ###################### input an anonymous function, ignore case groups for key and group in itertools. groupby ('aaabbbccaa', lambda c: c. upper (): print key, list (group) ###################### A ['A ', 'A', 'a'] # B ['B', 'B', 'B'] # C ['C ', 'C'] # A ['A', 'A', 'a'] ##########################

4. The difference between imap and map is that imap can act on infinite sequences.

Note: imap returns an iteration object, while map returns list

Import itertoolsfor x in itertools. imap (lambda x, y: x * y, [10, 20, 30], itertools. count (1): print x #10 40 90 # multiply the two values passed in by the anonymous function: 10*1, 20*2, 30*3
5. ifilter is the inert Implementation of filter.

Vi. XML

Use sax to parse xml:

from xml.parsers.expat import ParserCreateclass DefaultSaxHandler(object):    def start_element(self, name, attrs):        print('sax:start_element: %s, attrs: %s' % (name, str(attrs)))    def end_element(self, name):        print('sax:end_element: %s' % name)    def char_data(self, text):        print('sax:char_data: %s' % text)xml = r'''<?xml version="1.0"?><ol>    <li><a href="/python">Python</a></li>    <li><a href="/ruby">Ruby</a></li></ol>'''        handler = DefaultSaxHandler()parser = ParserCreate()parser.returns_unicode = Trueparser.StartElementHandler = handler.start_elementparser.EndElementHandler = handler.end_elementparser.CharacterDataHandler = handler.char_dataparser.Parse(xml)

VII. HTMLParser

Parse xml using python:

from HTMLParser import HTMLParserfrom htmlentitydefs import name2codepointclass MyHTMLParser(HTMLParser):    def handle_starttag(self, tag, attrs):        print('<%s>' % tag)    def handle_endtag(self, tag):        print('</%s>' % tag)    def handle_startendtag(self, tag, attrs):        print('<%s/>' % tag)    def handle_data(self, data):        print('data')    def handle_comment(self, data):        print('<!-- -->')    def handle_entityref(self, name):        print('&%s;' % name)    def handle_charref(self, name):        print('&#%s;' % name)parser = MyHTMLParser()parser.feed('

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.