简体   繁体   中英

For each value in 1 dictionary take keys and for those keys in 2 dictionary print difference in values for all specific pairs of keys

It's my first time here so sorry if I'm not conforming to some unspoken rule of the community So i have a dictionary mydict

{'ENST00000309502': ['ADORA1'],
 'ENST00000337894': ['ADORA1'],
 'ENST00000618295': ['ADORA1'],
 'ENST00000380573': ['ADRA1A'],
 'ENST00000519229': ['ADRA1A'],
 'ENST00000337474': ['AVPR2'],
 'ENST00000358927': ['AVPR2'],
 'ENST00000370049': ['AVPR2'],
 'ENST00000216629': ['BDKRB1'],
 'ENST00000611804': ['BDKRB1']...}

And for each specific value, I need to take the keys which are keys in my second dictionary mydict2

{'ENST00000216629':['3.61','3.45','3.65'...]
'ENST00000255380':['3.1','3.05','3.15'...]
'ENST00000304421':['3.61','3.15','3.65'...]...}

And i need to find difference in values of mydict2 for each of the key for the same value in mydict and print result like this

mydict[value] mydict2[key1] mydict2[key2] difference(list of values)

ADORA1 ENST00000309502 ENST00000337894 [3.61,3.25,3.14]

I only need one combination of difference so only 1-2 not 2-1 for some values in mydict there are more than two keys. Sorry for the long post I'm new in programming and I know I need for if loop, but im not sure how to write it. Thanks in advance for whoever helps.

In order to loop through all possible values in mydict , we need a set of values in mydict . Assuming that each value (eg ['ADORA1']) is a list with one element , we can flatten the list and create the set of values using

set_of_values = {value[0] for value in mydict.values()}

Then, we need the list of mydict keys corresponding to each value. We can use list comprehension and create a new dictionary mapping each mydict value to the list of mydict keys.

reversed_dict = {v:[key for (key, value) in mydict.items() if value[0] == v] for v in set_of_values}

The rest is straightforward. For each key in reversed_dict, we can print the corresponding mydict keys and mydict2 values.

for (key, values) in reversed_dict.items():
    print(f'{key} {values[0]} {values[1]} {mydict2[values[0]]} {mydict2[values[1]]}')

This will print

ADORA1 ENST00000309502 ENST00000337894 [3.61,3.25,3.14, ...] ['3.1','3.05','3.15'...]

for example.

It is unclear what a difference of a list is. If you need set difference, you can use python set using, for example,

set1 = set(mydict2[values[0]])
set2 = set(mydict2[values[1]])
print(set1 - set2)

I'm assuming you are looking for something like this:

# Loop over the "values" in mydict1 (i.e. the string in the value)
for value in {value[0] for value in mydict1.values()}:
    
    # Find the keys in mydict1 that correspond to the selected value
    keys = tuple(key for key in mydict1 if mydict1[key] == [value])

    # Loop over pairs of distinct keys
    num_of_keys = len(keys)
    for key1, key2 in {(keys[i], keys[j])
                       for i in range(num_of_keys)
                       for j in range(i+1, num_of_keys)}:

        # Check if both keys are keys in mydict2
        if (key1 in mydict2) and (key2 in mydict2):
            
            # Calculate the difference: The elements of the values (lists) 
            # of mydict2 are strings, not numbers: What do you mean by 
            # 'difference'?
            difference = [(float(mydict2[key1]) - float(mydict2[key2][i]))
                          for i in range(len(mydict2[key1]))]

            print(value, key1, key2, difference)

Some things are a bit odd:

  • Why are the values of mydict1 lists that contain only one string if you're actually interested in the string (it's not a problem, just a bit odd)?

  • What is the "difference"? The elements in the values of mydict2 (lists) are strings, not numbers?

EDIT : Okay, for that kind of "difference" I'd try:

    ....
        
        # Check if both keys are keys in mydict2
        if (key1 in mydict2) and (key2 in mydict2):
            
            # Select all values from mydict2[key1] that are not in
            # mydict2[key2]
            difference = set(mydict2[key1]).difference(set(mydict2[key2]))
            
            print(value, key1, key2, difference)

Or use a list comprehension:

    ....
        
        # Check if both keys are keys in mydict2
        if (key1 in mydict2) and (key2 in mydict2):
            
            # Select all values from mydict2[key1] that are not in
            # mydict2[key2]
            difference = [value
                          for value in mydict2[key1] 
                          if value not in mydict2[key2]]
            
            print(value, key1, key2, difference)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM