1

This may be a very naive question. I am trying to compare values of two dictionaries by:

distance = dictionary1[position] - dictionary2[position]

but it does not work, it gives me the first value of dictionary1.

I also tried with:

set distance = set(dictionary1[position]) - set(dictionary2[position])

but get the same result.

Any idea how values form two dictionaries can be subtracted?

Dictionaries 1 and 2 are like:

dictionary1 = {'chr1': '3434351', 'chr10': '5329532', 'chr11': '2355620',
               'chr12': '40841359', 'chr13': '101523938', 'chr14': '453255816',
               'chr17': '491453323', 'chr18': '22361089', 'chr19': '34965774',
               'chr2': '16025716', 'chr20': '295671539', 'chr21': '2067974'}

dictionary2 = {'chr1': '15845763', 'chr10': '186537818', 'chr11': '3715102',
               'chr12': '1138647613', 'chr13': '123235062', 'chr14': '68159413',
               'chr15': '51790735', 'chr16': '19324170', 'chr17': '78184979',
               'chr18': '76968073', 'chr19': '37299170', 'chr2': '18329102',
               'chr20': '31934245', 'chr22': '32679692'}
joaquin
  • 82,968
  • 29
  • 138
  • 152
user1287285
  • 11
  • 1
  • 5
  • 2
    Your question isn't quite clear to me. Please give an example input and output. – Sven Marnach Mar 31 '12 at 22:11
  • Can you post the output of `print dictionary1` and `print dictionary2`? – Blender Mar 31 '12 at 22:13
  • If the values in the dictionaries have subtraction defined on them then this should work. However it doesn't work so we need to know what kind of value is in the dictionaries. – aaronasterling Mar 31 '12 at 22:13
  • dictionary1 {'chr1': '3434351', 'chr10': '5329532', 'chr11': '2355620', 'chr12': '40841359', 'chr13': '101523938', 'chr14': '453255816', 'chr17': '491453323', 'chr18': '22361089', 'chr19': '34965774', 'chr2': '16025716', 'chr20': '295671539', 'chr21': '2067974',} dictionary2 {'chr1': '15845763', 'chr10': '186537818', 'chr11': '3715102', 'chr12': '1138647613', 'chr13': '123235062', 'chr14': '68159413', 'chr15': '51790735', 'chr16': '19324170', 'chr17': '78184979', 'chr18': '76968073', 'chr19': '37299170', 'chr2': '18329102', 'chr20': '31934245', 'chr22': '32679692', – user1287285 Mar 31 '12 at 22:20

3 Answers3

3

Using a dict comprehension:

{ k:int(dic1[k]) - int(dic2[k]) for k in dic1 if k in dic2 }

Or for Python < 2.7:

dict((k,int(dic1[k]) - int(dic2[k])) for k in dic1 if k in dic2)
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
Paolo
  • 20,112
  • 21
  • 72
  • 113
0

I had to use a workaround to solve the problem of keys which are not in both dictionaries. You should take care of this at your convenience. I set the value to zero when the key was not there.

keys = dictionary1.keys()
keys.extend(dictionary2.keys())

keys = set(keys)
dic = dict()
for key in keys:
    dic[key] = int(dictionary1.get(key,0)) - int(dictionary2.get(key,0))

print dic    

yields:

{'chr2': -2303386, 'chr1': -12411412, 'chr21': 2067974, 'chr13': -21711124, 'chr
12': -1097806254, 'chr11': -1359482, 'chr10': -181208286, 'chr17': 413268344, 'c
hr16': -19324170, 'chr15': -51790735, 'chr14': 385096403, 'chr20': 263737294, 'c
hr22': -32679692, 'chr19': -2333396, 'chr18': -54606984}

Edit As Niklas B. indicates in his comment you coud also use dictionary comprehensions. However they are not available in pythons < 2.7.

Community
  • 1
  • 1
joaquin
  • 82,968
  • 29
  • 138
  • 152
0
dictionary1 = {'chr1': '3434351', 'chr10': '5329532', 'chr11': '2355620',
             'chr12': '40841359', 'chr13': '101523938', 'chr14': '453255816'}
dictionary2 = {'chr1': '15845763', 'chr10': '186537818', 'chr11': '3715102',
             'chr12': '1138647613', 'chr13': '123235062', 'chr14': '68159413'}

dictionary3 = {}

# Assuming dictionary1 and dictoinary2 are the same size and have the same key
for key in dictionary1.keys():
    if key in dictionary1:
        if key in dictionary2:
            # Calculate the new value for dictionary3, by using int()
            newValue = int(dictionary1[key]) - int(dictionary2[key])
            dictionary3[key] = newValue
        else:
            print(key, 'is not in dictionary2')
    else:
        print(key, 'is not in dictionary1')

print(dictionary3)

Output

{'chr1': -12411412, 'chr13': -21711124, 'chr12': -1097806254, 
 'chr11': -1359482, 'chr10': -181208286, 'chr14': 385096403}

I think you can use int(), and then you can treat them as integers. I think it is one of the straight forward way to do this. Of course it is also good idea to provide checking to make sure both the same key (chr1x) exist, otherwise it will give you error, or you have to figure out something.

George
  • 4,514
  • 17
  • 54
  • 81