Python uses essays

Source: Internet
Author: User

In the process of using Python encountered a lot of problems, the knowledge point is scattered, through this paper to summarize.

One, Python's Itertools module

Combinations function: Combines the list by a given length.

 from Import  = [1, 2, 3, 4= [] for in range (1, len (seq) +1):    result.append (List ( Combinations (seq, i)))

Results: [(1,), (2,), (3,), (4,), (+), (1,3) ... (1,2,4), ... (1,2,3,4)]

The Product function:

 from Import  = List (product ('abc'xy'= List (product ( Range (3), repeat=2))

result1:[(' A ', ' X '), (' A ', ' Y '), (' B ', ' X '), (' B ', ' Y '), (' C ', ' X '), (' C ', ' Y ')]

result2:[(0,0), (0,1), (0,2), (1,0), (+), (+), (2,0), (2,1), (+ 2,2)]

Second, Python invokes the SOA service, using the Suds module

def Testservice ():     ' WSPL link Address of the service '      = suds. Client.client (URL)    = dict (x='123')    =  Client.service.METHOPNAME (param) Methopname: From the SOA service function name

Third, Python sorts the dictionary elements in the list

Sort_list = [{'datestamp'2017-05'}, {'  Datestamp'2017-04'}]sort_list.sort (Key= Operator.itemgetter ('datestamp'))

Iv. python conversion string encoding format

_str = _str.encode (encoding= ' UTF-8 ', errors= ' strict ')

V. Request data Module requests

When the requested URL requires username and password, you can use the following methods:

Import Requests def get_data (URL):     = requests. Session ()    = (username,  password)  # user name and password    s.headers.update ({'  x-test'true'})    = s.get (URL, headers={'x-test2'true'})    

Python uses essays

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.