Created
December 4, 2012 09:47
-
-
Save dulichan/4202251 to your computer and use it in GitHub Desktop.
Dictionary sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
@author: dulithar(Dulitha Ransanga Wijewantha) | |
Helping a Stack Overflow Question | |
a[0] = [0, 1, 2] | |
a[1] = [0, 2, 2] | |
a[2] = [100, 200, 300] | |
Should output as - [{0:2, 100:1}, {1:1, 2:1, 200:1}, {2:2, 300:1}] | |
Not sure about the efficiency of the code. But you can find better solutions at http://stackoverflow.com/q/13699169/813471 | |
''' | |
# | |
def compute(): | |
# This is a python tuple | |
a = ([0, 1, 2], [0, 2, 2], [100, 200, 300]) | |
computeA(a) | |
def computeA(array): | |
# This is a python dictionary | |
map = {} | |
# print array | |
for list in array: | |
for a in list: | |
value = 0 | |
if(map.has_key(a)): | |
value = map[a] | |
map[a] = value + 1 | |
k = '' | |
for n in map.items(): | |
k += str(n) | |
print k |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment