2014-05-20 63 views

回答

0

你能做到这一点,如下所示:

a = [('cat', 1), ('dog', 6)] 

x = float(sum(i[1] for i in a))/100 #in python3, you can just do: x = sum(i[1] for i in a)/100 

a = [(i,j/x) for i,j in a] 

>>> print a 
[('cat', 14.285714285714285), ('dog', 85.714285714285708)] 
1

也许类似

[(w, n/sum(x[1] for x in words)) for w, n in words] 

效率就留给读者自己练习。

+0

您仍然需要 “'float'它”(假设python2):HTTP:// REPL。 it/Swv – sshashank124

+0

@ sshashank124:如果您使用的是现代版本的Python,请不要使用它(请参阅[PEP 238](http://legacy.python.org/dev/peps/pep-0238/))。 –

-2

你可以考虑使用dict类型:

[{"cat":1},{"dog":6}] 

here something to read about dicts

+0

这是如何回答这个问题的? –

+0

这看起来更像是他想要的东西,而不是元组列表。他改变了他的问题。它之前是不同的 – Hans

相关问题