Python Forum
compare element of a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compare element of a dictionary
#1
Hello everyone

Here is an excerpt from my file I'm working on:

HS3ST1 GPC5 0 0 0 0 96 900 387 939
HS3ST1 GPC1 0 0 0 61 96 900 470 948 ---> EXP
HS3ST1 GPC4 0 0 0 0 96 900 462 947
SEMA3F PLXNA4 0 0 0 0 188 600 433 799
SEMA3F PLXNA3 0 0 0 62 188 600 900 965 ---> EXP
SEMA3F PLXNA1 0 0 0 111 188 600 904 968 ---> EXP
SEMA3F NRP1 0 0 0 0 478 800 839 981
SEMA3F PLXNA2 0 0 0 88 188 600 431 809 ---> EXP
CFTR PSMC2 0 0 0 62 59 900 50 905 ---> EXP
CFTR PSMD11 0 0 0 62 103 900 41 908 ---> EXP
CFTR DVL2 0 0 0 62 52 900 514 951 ---> EXP
CFTR CBL 0 0 0 0 379 900 207 946
CFTR PSMD4 0 0 0 0 51 900 87 905
CFTR EGFR 0 0 0 0 51 900 383 936


I have the following script:

from collections import defaultdict
Dprot1 = defaultdict(set)
Dprot2 = defaultdict(set)
Dinter = defaultdict(dict)
with open("C:/Users/lveillat/Desktop/Données stage/Données/resultats_matrice_avec_scores_sans_localisation.txt","r") as f1:
	for ligne in f1:
		lp = ligne.rstrip('\n').split(" ") 
		if lp[-1] == "EXP": 
			prot1 = lp[0] #prot1=First prot
			prot2 = lp[1] #prot2=Second prot
			if prot1 not in Dprot1: #if the protein is not in the dico then we add it (no duplicates)
				Dprot1[prot1].add(prot1) 
			if prot2 not in Dprot2: #same thing
				Dprot2[prot2].add(prot2)
			Dinter[prot1][prot2]=1 #If interaction between 2 prot on mark 1
	print (Dinter)
f1.close()

for i in Dprot1: #We go through all the elements of Dprot1
	for j in Dprot2: #We go through all the elements of Dprot2
		if Dinter[i][j]: #if i = prot1 and j = prot2
			print ("1")
		else:
			print("0")
Prot1 = different names of proteins, for example:
KDGHZ
DFGF
SDFET
SDGFT
SDDZ
BJJU
DAA
INS
ect...

Prot2 = Same
KQV
SHYZ
CKZ
YSC5E
2FQT
SGY2
ect…

Dinter[prot1][prot2]=1 = In key take a protein and value all the proteins with which it can interact, for example :

Output:
F5': {'SPTBN2': 1, 'ARFGAP1': 1, 'ANK2': 1, 'COPZ2': 1, 'KIF15': 1, 'KIF2B': 1, 'KIF25': 1, 'KDELR2': 1, 'RAB1B': 1, 'TMED10': 1, 'KIF3B': 1, 'GBF1': 1, 'CAPZB': 1, 'KIF3C': 1, 'COPA': 1, 'YKT6': 1, 'COPB1': 1, 'KIF20A': 1, 'RAB1A': 1, 'ACTR1A': 1, 'CAPZA2': 1, 'CAPZA1': 1, 'SPTBN5': 1, 'USO1': 1, 'CENPE': 1, 'KIFC1': 1, 'KIF9': 1, 'ARF1': 1, 'KIF6': 1, 'KIF18B': 1, 'KIF26A': 1, 'KIF5A': 1, 'KIF20B': 1, 'ANK1': 1, 'KIF18A': 1, 'KIF19': 1, 'KIF23': 1, 'BET1': 1, 'KDELR1': 1, 'ANK3': 1, 'SPTBN1': 1, 'KIF5B': 1, 'COPB2': 1, 'KIF11': 1, 'KIF2C': 1, 'ARCN1': 1, 'DYNLL2': 1, 'COPG2': 1, 'KIFC2': 1, 'COPG1': 1, 'KIF26B': 1, 'DYNLL1': 1, 'ARFGAP2': 1, 'KIF2A': 1, 'KIF22': 1, 'KIF12': 1, 'SEC22B': 1, 'ARFGAP3': 1, 'KDELR3': 1, 'KIF3A': 1, 'COPZ1': 1, 'COPE': 1, 'DCTN1': 1}, 'FKBP4': {'HSF1': 1, 'PPP5C': 1, 'ACTR1A': 1, 'HSPA1A': 1, 'PPID': 1, 'HSPA2': 1, 'PTGES3': 1, 'HSP90AA1': 1, 'HSP90AB1': 1, 'STIP1': 1, 'HSPA8': 1, 'EP300': 1, 'FOXA1': 1, 'HSPA1L': 1, 'ESR2': 1, 'MAPK3': 1, 'HSPA1B': 1, 'DCTN1': 1}
I am stuck at the end, it would be necessary that my algorithm by testing all possible pairs of proteins tells me 1 if there is interaction between the two proteins or 0 if there is none.

To do this, in the last lines of my algorithm, I have to compare the pair i, j with the pairs of proteins interacting with each other in my Dinter dictionary.

unfortunately it does not work with my algorithm, can you help me?

Thank you
Reply
#2
Please don't use i, l, or O as single digit variable names as they can look like numbers. To find keys common to both dictionaries:

for key in Dprot1: #We go through all the elements of Dprot1
    if key in Dprot2:
        print("intersection found")
You can also use 2 sets instead of dictionaries, and set.intersection
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 847 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  compare and find the nearest element ? mr_gentle_sausage 4 1,029 Jan-15-2023, 07:11 AM
Last Post: DPaul
  check if element is in a list in a dictionary value ambrozote 4 1,962 May-11-2022, 06:05 PM
Last Post: deanhystad
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,836 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,960 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  removing dictionary element in list using (key, value) MelonMusk 3 2,269 Jun-13-2020, 02:37 PM
Last Post: buran
  ValueError: dictionary update sequence element #0 has length 1; 2 Jmekubo 4 35,487 Apr-28-2019, 07:25 PM
Last Post: Jmekubo
  Unable to locate element no such element gahhon 6 4,453 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Converting List of 3 Element Tuple to Dictionary fooikonomou 11 5,806 Jan-14-2019, 09:51 AM
Last Post: perfringo
  Compare element of list with line of file : if match, delete line silfer 4 3,505 Jul-21-2018, 02:44 PM
Last Post: silfer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020