[Tutor] Help needed with Python programming

Steven D'Aprano steve at pearwood.info
Tue Apr 22 13:41:51 CEST 2014

On Mon, Apr 21, 2014 at 06:16:20PM -0700, Suhana Vidyarthi wrote:

> I have a python code that shows a set of shortest paths between nodes A and
> B. Now I have to select the least risky path among them. To do that I have
> to consider the risk values of each link. I know how to calculate the
> path's risk using its link value.
> For example: There is a path between node A and B wiht two links.
> Probability of failure for link 1 is 0.001 and for link 2 is 0.003. Here is
> the link with its risk values:
>                   A o--------------------o---------------------o B
>                                            0.001               0.003
> So the probability of the link being down will be: 1 - (0.999 x 0.997) =
> 0.996003

I don't think that calculation is correct. I think you mean that the 
probability of the link being UP is (0.999 x 0.997) = 0.996003, and the 
prob of it being DOWN is 1-0.996003 = 0.003997. So that path has a risk 
of 0.003997.

> You can find the attached file with disaster risk values of each link.
> For instance; first line is : 1,3,5,0.03   --> this means, first disaster
> affects links 1-3 and 5-0 and its occurrence rate is 0.03. So you need to
> assign link (1-3)'s risk to 0.03.
> Then you will continue with the next disaster which is the one in the next
> line. Note that, if a link gets affected by 2 disasters, you will add the
> probability of those 2 disasters to find that link's risk.
> If anyone can help me code the first line, I will be able to do the rest.
> You need use "array list" and some functions like "file reader" and
> "delimiter" I guess.

Okay, let's start with reading the file. 

filename = "path/to/file.txt"

Notice that I use forward slashes. Even if you are on Windows, you 
should code your paths with forward slashes. Either that, or you have to 
double every backslash:

# on Windows either of these will be okay
filename = "C:/path/to/file.txt"
filename = "C:\\path\\to\\file.txt"

Now let's read the file, one line at a time:

filename = "path/to/file.txt"
fp = open(filename, "r")
for line in fp:
    # process that single line

How might we process the line? I'm not sure what your requirements are, 
but at a guess you'll want something like this:

- ignore leading and trailing whitespace, including the end of 
  line marker at the end of each line;
- skip blank lines;
- split non-blank lines into four fields;
- convert the first three into integers;
- and the last field into a float.

filename = "path/to/file.txt"
fp = open(filename, "r")
for line in fp:
    # process that single line
    line = line.strip()  # ignore leading and trailing whitespace
    if not line:
        continue  # skip blank lines
    a, b, c, d = line.split(",")  # Split on commas
    a = int(a)  # convert to an int instead of string
    b, c = int(b), int(c)
    d = float(d)
    # And now you can handle the values a, b, c, d ...

And finally, when you are done, close the file:


Does this help?


More information about the Tutor mailing list