Skip to content

Instantly share code, notes, and snippets.

@dulichan
Created December 4, 2012 09:47
Show Gist options
  • Save dulichan/4202251 to your computer and use it in GitHub Desktop.
Save dulichan/4202251 to your computer and use it in GitHub Desktop.
Dictionary sum
'''
@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