2014-05-20 67 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

相關問題