程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Advanced Series (12)

編輯:Python

Catalog

lambda expression

A line of type


lambda expression

lambda An expression is a one line function .

They are also called anonymous functions in other languages . If you don't want to use a function twice in a program , You might want to use lambda expression , They are exactly the same as ordinary functions .

Prototype

    lambda Parameters : operation ( Parameters )

Example    

add = lambda x, y: x + y
    print(add(3, 5))

    Output: 8

There are also some lambda Application case of expression , Can be used in some special cases :

Sort the list    

 a = [(1, 2), (4, 1), (9, 10), (13, -3)]
    a.sort(key=lambda x: x[1])
    print(a)

 Output: [(13, -3), (4, 1), (1, 2), (9, 10)]

List parallel sort    

data = zip(list1, list2)
    data.sort()
    list1, list2 = map(lambda t: list(t), zip(*data))

A line of type

This chapter , I'm going to show you some one line Python command , These programs will be very helpful to you .

Simple and easy Web Server

Have you ever thought about sharing files quickly over the Internet ? The good news ,Python It provides you with such a function . Go to the directory where you want to share files and run the following code from the command line :

    Python 2:

 python -m SimpleHTTPServer

    Python 3:

python -m http.server

Beautiful print

You can Python REPL Print out lists and dictionaries beautifully . Here's the relevant code :   

 from pprint import pprint
    my_dict = {'name': 'Yasoob', 'age': 'undefined', 'personality'
    pprint(my_dict)

This method is more effective in dictionaries . Besides , If you want to print out documents quickly and beautifully json data , So you can do this :

cat file.json | python -m json.tool

Script performance analysis

This may be useful in locating performance bottlenecks in your scripts , It will work very well :

python -m cProfile my_script.py

remarks :cProfile It's a ratio. profile Faster implementation , Because it uses c Written

CSV Convert to json

Execute this command on the command line

python -c "import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv '))))

Make sure to replace csv_file.csv For what you want to convert csv file

List rolling

You can use the itertools In bag itertools.chain.from_iterable Easily and quickly flatten a list . Here is a simple example :   

 a_list = [[1, 2], [3, 4], [5, 6]]
    print(list(itertools.chain.from_iterable(a_list)))

     Output: [1, 2, 3, 4, 5, 6]

or

    print(list(itertools.chain(*a_list)))

 Output: [1, 2, 3, 4, 5, 6]\

One line constructors

Avoid a large number of repeated assignment statements during class initialization    

class A(object):
        def __init__(self, a, b, c, d, e, f):
            self.__dict__.update({k: v for k, v in locals().items()

For more one line methods, please refer to Python Official documents .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved