Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimizing a code
#3
Following is based on opening statement: "I create an algorithm that will give me the path of the shortest interactions between 2 proteins".

I would read data from file into dictionary or defaultdict so that key is node and values are edges.

With dictionary:

data = dict()                                                              
with open('proteins.txt', 'r') as f: 
    for row in f: 
        k, v = row.strip().split() 
        data.setdefault(k, set()).add(v)
Or with defauldict:

data = defaultdict(set)
with open('proteins.txt', 'r') as f: 
    for row in f: 
        k, v = row.strip().split() 
        data[k].add(v)
Now when I have data nicely structured I implement shortest path algorithm. You can find one from python. org: Python Patterns - Implementing Graphs.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Optimizing a code - by Amniote - Jul-11-2019, 02:11 PM
RE: Optimizing a code - by Gribouillis - Jul-11-2019, 03:48 PM
RE: Optimizing a code - by Amniote - Jul-11-2019, 04:08 PM
RE: Optimizing a code - by perfringo - Jul-11-2019, 03:57 PM
RE: Optimizing a code - by Gribouillis - Jul-11-2019, 05:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  do you have an idea for optimizing this code? netanelst 3 1,389 May-22-2022, 10:30 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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