`

python tips

阅读更多
1、enum
#!/usr/bin/env python
# -*- coding:utf-8 -*-

def enum(**enums):
    return type('Enum', (), enums)
Gender = enum(MALE=0,FEMALE=1)
print Gender.MALE
print Gender.FEMALE


2、检查字符串是否是number
s='123456789'
s.isdigit()#return True

3、list取交集
s=[1,2,3]
w=[2,3,4]
list(set(s).intersection(w))

4、两个list转成一个dict
dict(zip(a,b))


5、singleton
def singleton(cls):
    instances = {}
    def get_instance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return get_instance

第二种tornado IOLoop中使用的单例模式:
    @staticmethod
    def instance():
        """Returns a global IOLoop instance.

        Most single-threaded applications have a single, global IOLoop.
        Use this method instead of passing around IOLoop instances
        throughout your code.

        A common pattern for classes that depend on IOLoops is to use
        a default argument to enable programs with multiple IOLoops
        but not require the argument for simpler applications::

            class MyClass(object):
                def __init__(self, io_loop=None):
                    self.io_loop = io_loop or IOLoop.instance()
        """
        if not hasattr(IOLoop, "_instance"):
            with IOLoop._instance_lock:
                if not hasattr(IOLoop, "_instance"):
                    # New instance after double check
                    IOLoop._instance = IOLoop()
        return IOLoop._instance

6、list排重
{}.fromkeys(list).keys()


未完待续...

参考资料:
http://stackoverflow.com/questions/36932/whats-the-best-way-to-implement-an-enum-in-python
http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-in-python
http://docs.python.org/2/library/stdtypes.html#str.isdigit
分享到:
评论

相关推荐

    Python-PythonTips一些初学者到中级用户的Python技巧

    Python Tips 一些初学者到中级用户的Python技巧

    leetcode2sumc-solutions:LeetCode及PythonTips部分题目求解

    pythonTips 上的题解 LeetCode主页:.已做题目数: 47 力码 数字 标题 困难 语 运行 头脑 01 二和 简单的 1138ms/34.16% 列表转字典,哈希表 07 反转整数 简单的 Java 33 毫秒/5.42% 09 回文数 简单的 Java 154ms/...

    小费数据集

    利用Python进行数据分析,小费数据集。

    Python Tips系列1

    各种python小技巧: 1. 列表相减:  列表A是一个完整轨迹,B是已走过的轨迹,求剩下的轨迹 # A: ['a','b','c','d'] # B: ['a','b'] In [89]: A = ['a','b','c','d'] In [90]: B = ['a','b'] In [92]: [x for x ...

    PythonTips:此仓库包含在http上发布的所有教程

    此仓库包含在发布的所有教程 ####编辑 我已经有一段时间没有更新这个仓库了。

    python-tips-tricks:Python技巧与窍门

    Python技巧与窍门有关Python的一些技巧和窍门。有用的文件-BeautifulSoup库的概述,以便解析XML文件。 -概述Oletools库,以便检查VB代码 -Pyspark库概述-Sweetviz库概述帮助您找到所有缺少的值并显示它们-获取文件的...

    五个Python编程Tips,帮你提高编码效率.doc

    五个Python编程Tips,帮你提高编码效率.doc

    Data-Wrangling-with-Python-Tips-and-Tools-to-Make-Your-Life-Easier.pdf.pdf

    Data-Wrangling-with-Python-Tips-and-Tools-to-Make-Your-Life-Easier.pdf

    Illustrated Guide to Python 3

    Packed with Useful Hints and Tips You'll learn the best practices without wasting time searching or trying to force Python to be like other languages. I've collected all the gems I've gleaned over ...

    Serious Python

    An indispensable collection of practical tips and real-world advice for tackling common Python problems and taking your code to the next level. Features interviews with high-profile Python developers ...

    tips.txt为python数据分析案例数据

    python数据分析案例数据 主要用于本人博客的部分文章案例数据使用。 单变量的样本分布检验(python3) 探索变量间关系 ....... 等文章的案例数据

    Clean Python.pdf

    The main goal of this book is to provide tips to Python developers of various levels so they can write better Python software and programs. This book gives you various techniques irrespective of the ...

    tips_python_

    python练习数据。机器人理财是将人工智能导入传统的理财顾问服务,并非由实体的机器人帮助客户理财,而是透过网络线上互动,依据需求者设定的投资目的及风险承受度,透过计算机程序的算法,提供自动化的投资组合建议...

    Python.Unit.Test.Automation.pdf

    Quickly learn how to automate unit testing of Python 3 code with Python 3 automation libraries, such as doctest, unittest, nose, nose2, and pytest. This book explores the ...Chapter 6: Tips and Tricks

    自动办公实用tips Python自动发送邮件

    使用Python实现自动化邮件发送,可以让你摆脱繁琐的重复性业务,可以节省非常多的时间。我们把报表做出来以后一般都是需要发给别人查看,对于一些每天需要发的报表或者是需要一次发送多份的报表,这个时候可以考虑...

    KBEngine-Python-Tips-master.zip

    这个项目是为了给KBEngine服务端编写Python脚本的时候,让IDE有语法提示。

    Data Wrangling with Python

    Data Wrangling with Python 全面掌握用Python进行爬虫抓取以及数据清洗与分析的方法 轻松实现高效数据处理 https://item.jd.com/12219342.html

    Python Graphics

    Python developers looking for tips on how to create illustrations and visualizations, as well as scientists, engineers, or students using Python. It assumes familiarity with vectors, matrices, ...

    Python小tips

    收录python基础库中sys、pathlib、arrow、configParser等基础库的用法,以及其它基础知识

    英文原版-Programming Python Programming Guide For Beginners Learn In a Day 2nd Edition

    These fun and easy tips transform the dreaded chore of learning a new programming language into a fun process. You'll be proud to show off your new skills to your friends and family! What are the ...

Global site tag (gtag.js) - Google Analytics