不是专门在Python中,但是尝试使用以下命令:
如果将该数据加载到python(csv.reader(, delimiter='\t')),则可以执行加权概率类型函数,以返回该列表中昵称的全名。
你可以这样做:import collections
def weighted_choice_sub(weights):
# Source for this function:
# http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/
rnd = random.random() * sum(weights)
for i, w in enumerate(weights):
rnd -= w
if rnd < 0:
return i
def load_names():
with open(, 'r') as infile:
outdict = collections.defaultdict(list)
for line in infile.readlines():
tmp = line.strip().split('\t')
outdict[tmp[0]].append((tmp[1], float(tmp[2])))
return outdict
def full_name(nickname):
names = load_names()
return names[nickname][weighted_choice_sub([x[1] for x in names[nickname]])][0]