Python 有权重的随机选择, Weighted Random Choice
import random def windex(lst):
'''an attempt to make a random.choose() function that makes weighted choices
accepts a list of tuples with the item and probability as a pair'''
wtotal = sum([x[1] for x in lst])
n = random.uniform(0, wtotal)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item
相关文档:
type相关:
所有自定义类A
其实例,如 a = A()
使用type运算符返回的都是<type, 'instance'>
而基本类型
比如 b = 3
type(b),结果是<type, 'int'>
python的type不像C++中的typeid那样,可以显示类名。
(注:对于没有virtual函数的类而言,typeid是编译时期的事情(也就是静态类型);对于有virtual函 ......
原帖:http://www.cnblogs.com/jingleguo/archive/2008/06/02/1211820.html
当python中间处理非ASCII编码时,经常会出现如下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128)
0x??是超出128的数字,python在默认的情况下认为语言的编码是ascii编码,所以无法 ......
这是一个简单的表达式计算器, 不太懂用Tkinter写GUI, 参考了别人的代码
from __future__ import division
from Tkinter import Tk, Entry, Button, Label, mainloop
from tkFont import Font
def get_value ():
v = ''
try:
v = eval(text.get()) #use eval to calculate the vlaue of the text
except:
pa ......
1.常用方法,不带参数
def decator(func):
def inner_func(*args):
args = (i * 2 for i in args)
return func(*args)
return inner_func
@decator
def add(a, ......