Thanks for the response Alan. my clarifications are below:
On Sat, Apr 26, 2014 at 1:41 AM, Alan Gauld <[email protected]>wrote:
> On 26/04/14 01:46, Suhana Vidyarthi wrote:
>
> I have this file:
>>
>> 1,3,5,0.03
>>
>> 2,3,5,5,4,0.11
>>
>> 3,3,5,5,4,5,8,0.04
>>
> ....
>
>> And each line is interpreted as:
>>
>> * 1,3,5,0.03-> This line means 1 link can be down i.e. between 3—5
>> with a probability of failure *0.03*
>> * 2,3,5,5,4,0.11 -> This line means 2 links can be down i.e. between
>> 3--5 and 5--4 with a probability of failure *0.11* (for each link)
>> * 3,3,5,5,4,5,8,0.04 -> Similarly this line means 3 links can be down
>> i.e. between 3--5 , 5—4 and 5—8 with a probability of failure *0.04*
>> (for each link)
>>
> ...
>
>> I want to create two arrays using the above file (Links array and Prob
>> array) that should give following output:
>>
>> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18]
>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20]
>> [15,20] [21,20] [20,21] [21,16] [21,22] }
>>
>> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08]
>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]}
>
>
> I don't understand how you develop this? The first list has 22 items the
> second 17. I would have expected them to be the same?
>
In the "Prob" array the elements are less because if you read the note
below: I said the links that are repeating for example [3,5] their
probabilities get added and stored as a single value in the "Prob" array.
>
> Also why do you want these two lists? What do you plan on doing with them?
> I would have thought a mapping of link to probability would be much more
> useful? (mapping => dictionary)
I want these two lists because using the links and probs array, I will
check which link has the lowest probability. Here lowest probability means
the risk of failure for that link. So based on which link has least
probability of failure, I will use it to setup a connection (I have a
source and destination and the links mentioned above are the paths between
them) I want to select the path which has least failure probability.
Did it make sense?
>
> So the first element in Links array is [3,5] and its probability of
>> failure is the first element in Prob array i.e. 0.28
>>
>> Can anyone help me with this please?
>>
>
> Do you know how to open and read a file line by line?
> Do you know how to extract the elements from a line?
> Do you know how to define a list and add elements to it?
> Do you know how to find an element in a list?
> Do you know how to modify an element in a list?
>
> If you know the above you have all the pieces you need to complete the
> task. If not tell us which bits you are stuck with.
>
> I have been studying about it and have come up with my code.
> And show us some code, we will not do your work for you but are happy to
> help.
>
> I actually used the map and dictionary function to write the code, please
see the attachment. The only problem I am having is that when I try to
display the links and probabilities, they are not displayed in the order
of the file content. This is how it should display:
Links = { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] [10,13]
[14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20]
[15,20] [21,20] [20,21] [21,16] [21,22] }
Prob = {[0.18] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08]
[0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]}
However when I run my code, this is how the arrays are displayed:
Links ->
[('10', '13'), ('14', '18'), ('7', '8'), ('15', '20'), ('5', '8'), ('5',
'4'), ('11', '9'), ('21', '22'), ('12', '13'), ('21', '20'), ('17', '13'),
('20', '21'), ('21', '16'), ('14', '10'), ('11', '12'), ('11', '19'),
('14', '13'), ('3', '5'), ('11', '6'), ('19', '20')]
Probability ->
[0.04, 0.06, 0.04, 0.24, 0.08, 0.15, 0.08, 0.27, 0.04, 0.29, 0.08, 0.27,
0.27, 0.04, 0.08, 0.08, 0.08, 0.18000000000000002, 0.08, 0.24]
Can you please see my code and help me find out what is wrong?
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
> _______________________________________________
> Tutor maillist - [email protected]
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
with open('/Users/suhana/Downloads/disastersWMD_ATT.txt', 'r') as content_file:
# print content_file.read()
#from collections import defaultdict
newdict = dict()
linedict = {}
#links array
links_array = []
#probability array
prob_array = []
#this gets executed for no of lines in files
for line in content_file:
#print line.strip()
data = line.split(",")
#print data
data_Len = len(data)
#print data_Len
j = data[0]
# print j
LastItem = data[data_Len-1];
#print LastItem
#data[1:-1] gives all array contents except 1st and last
#data[-1] gives last element of array
links, prob = data[1:-1], data[-1]
#dictionary for check if element already present
for x in range(0, int(j)):
prob_array.append(prob)
#this for loop starts from 0 till length of array and increments by 2
#and stores in []
links = [links[i:i+2] for i in range(0, len(links), 2)]
for i in range(0, len(links), 1):
#print links[i]
if tuple(links[i]) not in linedict:
linedict[tuple(links[i])] = [prob]
else:
linedict[tuple(links[i])].append(prob)
#for key in linedict:
#print "%s: %s" % (key, linedict[key])
for key in linedict.keys():
linedict[key]=sum(map(float, linedict[key]))
#for key in linedict:
#print "%s: %s" % (key, linedict[key])
list_Links = []
list_Prob = []
for key in linedict:
list_Links.append(key)
list_Prob.append(linedict[key])
print "Links ->"
print list_Links
print ""
print "Probability ->"
print list_Prob
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor