1. Aufzählung
Python-Code
#!/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. Überprüfen Sie, ob die Zeichenfolge eine Zahl ist
Python-Code
s='123456789' s.isdigit()#return True
3. Überschneidungsliste
Python-Code
s=[1,2,3] w=[2,3,4] list(set(s).intersection(w))
4. Konvertieren Sie zwei Listen in ein Diktat
Python-Code
dict(zip(a,b))
5. Python-Code
def singleton(cls): instances = {} def get_instance(): if cls not in instances: instances[cls] = cls() return instances[cls] return get_instance
Python-Code
@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
Python-Code
{}.fromkeys(list).keys()