Re: [Tutor] trying to understand pattern matching code
Danny and Dave, super thanks on the plain text advice. i'm sending this mail on plain text, please let me know if it turned out to be otherwise back to the problem just googled binary and i now have some idea on it. i now understand turning on 16 and 8 gives 24 and thus 4+8 = 12. why is this? i now understand this bit. but i still couldnt get this line though D = (( D << 1) + 1) & masks [ c ] On Sat, Apr 26, 2014 at 12:37 AM, Dave Angel wrote: > rahmad akbar Wrote in message: >> >> > to Dave, >> i do i do the text mode? i had no idea this was on html > > Whatever mail program you're using is apparently defaulting to > html. Find a menu item that specifies 'text' or 'plain text' or > 'text only'. > > Or tell us what email program you're using, what os, and versions > for each. Maybe someone will have experience with > it. > > Back to your problem, do you understand what binary is, and how > to convert (mentally) to and from decimal? Do you know that the > bits have values of 1, 2, 4, 8, 16, etc? And that 24 is created > by turning on the 16 bit and the 8 bit, commonly referred to as > bits 4 and 3, respectively. > > > -- > DaveA > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- many thanks mat ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
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? 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) 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. And show us some code, we will not do your work for you but are happy to help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] xlrd package
On 26/04/14 05:48, Sunil Tech wrote: I want to know how many sheets can be created in Excel using xlrd package. This list if for people learning the Python language and standard library. xlrd is not part of that so you may have more success asking on an xlrd forum. Or failing that a Windows forum? Or you may just get lucky and find somebody here who has used xlrd... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trying to understand pattern matching code
On 26/04/14 09:36, rahmad akbar wrote: but i still couldnt get this line though D = (( D << 1) + 1) & masks [ c ] Do you understand the concept of bit shifting? ie 000110 shifted left gives 001100 and 000110 shifted right gives 11 In other words the bit pattern moves left or right and the missing bits are replaced with zeros. The effect of shift left is to multiply the number by two. + 1 just adds 1 to the resulting number So the first bit of your line is the same as D = ((D*2)+1) The second part uses bitwise and with a mask chosen from a list of masks, A bitwise and has the effect of zeroing any bit that is zero in the mask and keeping any bit that is one in the mask. So 11100011 -> My data -> my mask, designed to return the right hand 4 bits 0011 -> data & mask Similarly 11100011 -> data -> mask for leftmost 4 bits 1110 -> data & mask So which bits are preserved in your D after the math and masking depends on what the masks[c] mask looks like. You might want to use some print statements using the bin() function to see the actual bit patterns. (If you do you might find that long bit patterns don;t show what you expect, if that happens try masking the result to say 16 places like this: print bin(mydata & 0x) # => HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] xlrd package
On 26/04/2014 10:43, Alan Gauld wrote: On 26/04/14 05:48, Sunil Tech wrote: I want to know how many sheets can be created in Excel using xlrd package. This list if for people learning the Python language and standard library. xlrd is not part of that so you may have more success asking on an xlrd forum. Or failing that a Windows forum? Or you may just get lucky and find somebody here who has used xlrd... Hello, xlrd is for reading xl sheets not for writing. xlwt is for writing Excel sheets. I think there is "no human limit" to create hundred. I use only xlrd. for reading to create csv (ascii) documents per existing sheet. Regards ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] xlrd package
On 26/04/2014 05:48, Sunil Tech wrote: Hi, I want to know how many sheets can be created in Excel using xlrd package. Thank you By using your favourite search engine you could have found this http://www.python-excel.org/ and hence this https://groups.google.com/forum/#!forum/python-excel -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
Hi Suhana, Also note that you asked this question just a few days ago. https://mail.python.org/pipermail/tutor/2014-April/101019.html We're not robots. We don't like repetition unless there's a reason for it, and in this case, you got responses to the earlier question. For example: https://mail.python.org/pipermail/tutor/2014-April/101022.html https://mail.python.org/pipermail/tutor/2014-April/101029.html Did you see these responses? If not, please check your mail settings. If you want to continue working on this problem, I'd recommend continuing that thread rather than start a fresh one. Reason is, if I were to look at your question fresh, I'd answer the exact same way to it. I'm trying to lightly probe what you've done and what you understand already. If you're repeating the question in the hopes that repetition will wear down the people you're trying to get answers from, please change your learning strategy: it won't be effective here. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trying to understand pattern matching code
hi Alan, your explanation clears most things, super thanks!! On Sat, Apr 26, 2014 at 10:58 AM, Alan Gauld wrote: > On 26/04/14 09:36, rahmad akbar wrote: > >> but i still couldnt get this line though >> >> D = (( D << 1) + 1) & masks [ c ] > > > Do you understand the concept of bit shifting? > ie > 000110 shifted left gives > 001100 > > and > 000110 shifted right gives > 11 > > In other words the bit pattern moves left or > right and the missing bits are replaced with > zeros. > > The effect of shift left is to multiply the > number by two. > > + 1 just adds 1 to the resulting number > > So the first bit of your line is the same as > > D = ((D*2)+1) > > The second part uses bitwise and with a mask chosen from a list of masks, > A bitwise and has the effect of zeroing any bit that is zero in the mask and > keeping any bit that is one in the mask. > > So > > 11100011 -> My data > -> my mask, designed to return the right hand 4 bits > 0011 -> data & mask > > Similarly > 11100011 -> data > -> mask for leftmost 4 bits > 1110 -> data & mask > > So which bits are preserved in your D after the math and masking depends on > what the masks[c] mask looks like. > > You might want to use some print statements using the bin() > function to see the actual bit patterns. (If you do you might > find that long bit patterns don;t show what you expect, if > that happens try masking the result to say 16 places > like this: > > print bin(mydata & 0x) # => > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- many thanks mat ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
Hi, The reason I opened a link is because there are changes in the code. Does it make sense? Else I can definitely go back to the thread. On Sat, Apr 26, 2014 at 9:05 AM, Danny Yoo wrote: > Hi Suhana, > > Also note that you asked this question just a few days ago. > > https://mail.python.org/pipermail/tutor/2014-April/101019.html > > We're not robots. We don't like repetition unless there's a reason > for it, and in this case, you got responses to the earlier question. > For example: > > https://mail.python.org/pipermail/tutor/2014-April/101022.html > https://mail.python.org/pipermail/tutor/2014-April/101029.html > > Did you see these responses? If not, please check your mail settings. > > > If you want to continue working on this problem, I'd recommend > continuing that thread rather than start a fresh one. Reason is, if I > were to look at your question fresh, I'd answer the exact same way to > it. I'm trying to lightly probe what you've done and what you > understand already. > > If you're repeating the question in the hopes that repetition will > wear down the people you're trying to get answers from, please change > your learning strategy: it won't be effective here. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
Thanks for the response Alan. my clarifications are below: On Sat, Apr 26, 2014 at 1:41 AM, Alan Gauld 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.18002, 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 - Tutor@python.org > 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
Re: [Tutor] Help needed
>>> 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. But what will you plan to do with these values afterwards? I think Alan's point here is that if there's no direct relationship between the elements in Links and the elements in Probs, those values aren't going to be very useful to solve the rest of the problem. One way to look at this problem is to simplify or normalize the input; the original structure in the file is slightly weird to process, since a single line of the input represents several link/failure pairs. One concrete example is: 4,10,13,14,13,17,13,12,13,0.04 where all these numbers are uninterpreted. You can imagine something that takes the line above, and breaks it down into a series of LinkFailure items. ## class LinkFailure(object): """Represents a link and the probability of failure.""" def __init__(self, start, end, failure): self.start = start self.end = end self.failure = failure ## which represent a link and failure structure. If we have a structure like this, then it explicitly represents a relationship between a link and its failure, and the string line: 4,10,13,14,13,17,13,12,13,0.04 can be distilled and represented as a collection of LinkFailure instances: [LinkFailure(10, 13, 0.04), LinkFailure(14, 13, 0.04), LinkFailure(17, 13, 0.04), LinkFailure(12, 13, 0.04)] Then the relationship is explicit. >> 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? Unfortunately, I'm still confused. If you just have the Links and the Probs lists of unequal length, unless there's some additional information that you're represented, then I don't see the necessary connection between the two lists that lets you go any further in the problem. There's no one-to-one-ness: given a link in Links, which Probs do you want to look at? If you don't represent that linkage in some way, I don't understand yet where you go next. >>> 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 But if the two lists are different lengths, what probability of failure is associates with the last element in the Prob array? The representation of data is important: if you choose an awkward representation, it makes solving this problem more difficult than it needs be. That is, if you're already compressing multiple elements in Prob that correspond to the same link, you also need some way to figure out what link that a compressed probability refer to. Otherwise, you don't have enough information to solve the problem anymore. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
Hi Danny, Let me give you a high level brief of what I am doing: I am working on doing "disaster aware routing" considering the 24-node US network where I will be setting up connection between two any two nodes (I will select the source and destination nodes randomly). Also I have some links whose "probability of failure" is mentioned in the attached file. Other links, which are not mentioned in the file - we suppose their "probability of failure" is zero. So between the source-destination nodes, there will be multiple paths and I will select the one which has "least probability of failure". Now to setup the connection between two nodes, I have to select a path whose "probability of failure" is least. To do that first I will calculate the risk of each path from the attached file and then select the path with least risk value. Did you get this part? I know it can be a bit confusing. Now I break the problem into parts: 1. I have to topology of the 24-node map 2. I have the link values of each link - where risk values are the "probability of failure" 3. I calculate the total "probability of failure" of each path (a path may have multiple links): Suppose my source node is "a" and destination node is "b". I can setup a path between a to b via c or via d (a-c-b or a-d-c): Here I will check the risk values of a-c and c-b; also risk values of a-d and d-c. If the total risk valure of a-c-b is lower that risk value of a-d-c, then I select the path a-c-d to setup the connection. (again risk value = probability of failure) Now, I will first calculate the "total probability of failure" of each link (using the file.txt) and since some links are repeated their values will be added. The probabilities get added if a link is mentioned twice or thrice. For example: link 3—5 is repeated 3 times: in line one, it has a probability of failure as 0.03, in line two it is 0.11 and in line three it is 0.04. So the probability of failure for link 3—5 is 0.03+0.11+0.04 = 0.18 The length of each array will be same. You see the code I wrote: here is the output for it: 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.18002, 0.08, 0.24] It means that link [10,13] has a "probability of failure" as [0.04] and since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and 0.04, its "probability of failure" is [0.18] (third last element in the Probability array). For some reason instead of 0.18 it is showing 0.1802, which I cannot figure to why. Please see the attached code. If you see the file.txt and my output: the output is not displayed in sequence and that is what I need help with. I want this to 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]} If you can figure why the output is not generated in same sequence as in the file.txt for me, it will be very helpful. let me know if I explained correctly, and if you have any questions or doubts? On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: > >>> 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. > > > But what will you plan to do with these values afterwards? I think > Alan's point here is that if there's no direct relationship between > the elements in Links and the elements in Probs, those values aren't > going to be very useful to solve the rest of the problem. > > > One way to look at this problem is to simplify or normalize the input; > the original structure in the file is slightly weird to process, since > a single line of the input represents several link/failure pairs. > > One concrete example is: > > 4,10,13,14,13,17,13,1
Re: [Tutor] Help needed
Just glancing at your work, I see you have curly braces around what looks like it should be a list. If you are concerned with the order of your output, dictionaries do not have a concept of order. On Sat, Apr 26, 2014 at 3:16 PM, Suhana Vidyarthi wrote: > Hi Danny, > > Let me give you a high level brief of what I am doing: > I am working on doing "disaster aware routing" considering the 24-node US > network where I will be setting up connection between two any two nodes (I > will select the source and destination nodes randomly). Also I have some > links whose "probability of failure" is mentioned in the attached file. > Other links, which are not mentioned in the file - we suppose their > "probability of failure" is zero. So between the source-destination nodes, > there will be multiple paths and I will select the one which has "least > probability of failure". > > Now to setup the connection between two nodes, I have to select a path > whose "probability of failure" is least. To do that first I will calculate > the risk of each path from the attached file and then select the path with > least risk value. Did you get this part? I know it can be a bit confusing. > > Now I break the problem into parts: > > 1. I have to topology of the 24-node map > 2. I have the link values of each link - where risk values are the > "probability of failure" > 3. I calculate the total "probability of failure" of each path (a path may > have multiple links): Suppose my source node is "a" and destination node is > "b". I can setup a path between a to b via c or via d (a-c-b or a-d-c): > Here I will check the risk values of a-c and c-b; also risk values of a-d > and d-c. If the total risk valure of a-c-b is lower that risk value of > a-d-c, then I select the path a-c-d to setup the connection. (again risk > value = probability of failure) > > Now, I will first calculate the "total probability of failure" of each > link (using the file.txt) and since some links are repeated their values > will be added. The probabilities get added if a link is mentioned twice > or thrice. For example: link 3—5 is repeated 3 times: in line one, it > has a probability of failure as 0.03, in line two it is 0.11 and in line > three it is 0.04. So the probability of failure for link 3—5 is > 0.03+0.11+0.04 = 0.18 > > The length of each array will be same. You see the code I wrote: here is > the output for it: > > 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.18002, 0.08, 0.24] > > > It means that link [10,13] has a "probability of failure" as [0.04] and > since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and > 0.04, its "probability of failure" is [0.18] (third last element in the > Probability array). For some reason instead of 0.18 it is showing > 0.1802, which I cannot figure to why. > > > Please see the attached code. If you see the file.txt and my output: the > output is not displayed in sequence and that is what I need help with. I > want this to 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]} > > > If you can figure why the output is not generated in same sequence as in > the file.txt for me, it will be very helpful. > > > let me know if I explained correctly, and if you have any questions or > doubts? > > > On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: > >> >>> 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. >> >> >> But what will you plan to do with these values afterwards? I think >> Alan's point here is that if there's no direc
Re: [Tutor] Help needed
err, set also is unordered. I can see you are using set for a reason, but has no concept of order. On Sat, Apr 26, 2014 at 3:20 PM, C Smith wrote: > Just glancing at your work, I see you have curly braces around what looks > like it should be a list. If you are concerned with the order of your > output, dictionaries do not have a concept of order. > > > On Sat, Apr 26, 2014 at 3:16 PM, Suhana Vidyarthi < > suhanavidyar...@gmail.com> wrote: > >> Hi Danny, >> >> Let me give you a high level brief of what I am doing: >> I am working on doing "disaster aware routing" considering the 24-node US >> network where I will be setting up connection between two any two nodes (I >> will select the source and destination nodes randomly). Also I have some >> links whose "probability of failure" is mentioned in the attached file. >> Other links, which are not mentioned in the file - we suppose their >> "probability of failure" is zero. So between the source-destination nodes, >> there will be multiple paths and I will select the one which has "least >> probability of failure". >> >> Now to setup the connection between two nodes, I have to select a path >> whose "probability of failure" is least. To do that first I will calculate >> the risk of each path from the attached file and then select the path with >> least risk value. Did you get this part? I know it can be a bit confusing. >> >> Now I break the problem into parts: >> >> 1. I have to topology of the 24-node map >> 2. I have the link values of each link - where risk values are the >> "probability of failure" >> 3. I calculate the total "probability of failure" of each path (a path >> may have multiple links): Suppose my source node is "a" and destination >> node is "b". I can setup a path between a to b via c or via d (a-c-b or >> a-d-c): Here I will check the risk values of a-c and c-b; also risk values >> of a-d and d-c. If the total risk valure of a-c-b is lower that risk value >> of a-d-c, then I select the path a-c-d to setup the connection. (again risk >> value = probability of failure) >> >> Now, I will first calculate the "total probability of failure" of each >> link (using the file.txt) and since some links are repeated their values >> will be added. The probabilities get added if a link is mentioned twice >> or thrice. For example: link 3—5 is repeated 3 times: in line one, it >> has a probability of failure as 0.03, in line two it is 0.11 and in line >> three it is 0.04. So the probability of failure for link 3—5 is >> 0.03+0.11+0.04 = 0.18 >> >> The length of each array will be same. You see the code I wrote: here is >> the output for it: >> >> 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.18002, 0.08, 0.24] >> >> >> It means that link [10,13] has a "probability of failure" as [0.04] and >> since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and >> 0.04, its "probability of failure" is [0.18] (third last element in the >> Probability array). For some reason instead of 0.18 it is showing >> 0.1802, which I cannot figure to why. >> >> >> Please see the attached code. If you see the file.txt and my output: the >> output is not displayed in sequence and that is what I need help with. I >> want this to 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]} >> >> >> If you can figure why the output is not generated in same sequence as in >> the file.txt for me, it will be very helpful. >> >> >> let me know if I explained correctly, and if you have any questions or >> doubts? >> >> >> On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: >> >>> >>> 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
Re: [Tutor] Help needed
Thanks for the response Smith, I was thinking make be I have done something incorrect and if there is some other function that can be used to display the output in desired order but don't see it possible thats why was wondering if any of you Python gurus have any inputs for me :-) On Sat, Apr 26, 2014 at 12:36 PM, C Smith wrote: > err, set also is unordered. I can see you are using set for a reason, but > has no concept of order. > > > On Sat, Apr 26, 2014 at 3:20 PM, C Smith wrote: > >> Just glancing at your work, I see you have curly braces around what looks >> like it should be a list. If you are concerned with the order of your >> output, dictionaries do not have a concept of order. >> >> >> On Sat, Apr 26, 2014 at 3:16 PM, Suhana Vidyarthi < >> suhanavidyar...@gmail.com> wrote: >> >>> Hi Danny, >>> >>> Let me give you a high level brief of what I am doing: >>> I am working on doing "disaster aware routing" considering the 24-node >>> US network where I will be setting up connection between two any two nodes >>> (I will select the source and destination nodes randomly). Also I have some >>> links whose "probability of failure" is mentioned in the attached file. >>> Other links, which are not mentioned in the file - we suppose their >>> "probability of failure" is zero. So between the source-destination nodes, >>> there will be multiple paths and I will select the one which has "least >>> probability of failure". >>> >>> Now to setup the connection between two nodes, I have to select a path >>> whose "probability of failure" is least. To do that first I will calculate >>> the risk of each path from the attached file and then select the path with >>> least risk value. Did you get this part? I know it can be a bit confusing. >>> >>> Now I break the problem into parts: >>> >>> 1. I have to topology of the 24-node map >>> 2. I have the link values of each link - where risk values are the >>> "probability of failure" >>> 3. I calculate the total "probability of failure" of each path (a path >>> may have multiple links): Suppose my source node is "a" and destination >>> node is "b". I can setup a path between a to b via c or via d (a-c-b or >>> a-d-c): Here I will check the risk values of a-c and c-b; also risk values >>> of a-d and d-c. If the total risk valure of a-c-b is lower that risk value >>> of a-d-c, then I select the path a-c-d to setup the connection. (again risk >>> value = probability of failure) >>> >>> Now, I will first calculate the "total probability of failure" of each >>> link (using the file.txt) and since some links are repeated their values >>> will be added. The probabilities get added if a link is mentioned twice >>> or thrice. For example: link 3—5 is repeated 3 times: in line one, it >>> has a probability of failure as 0.03, in line two it is 0.11 and in line >>> three it is 0.04. So the probability of failure for link 3—5 is >>> 0.03+0.11+0.04 = 0.18 >>> >>> The length of each array will be same. You see the code I wrote: here >>> is the output for it: >>> >>> 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.18002, 0.08, 0.24] >>> >>> >>> It means that link [10,13] has a "probability of failure" as [0.04] and >>> since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and >>> 0.04, its "probability of failure" is [0.18] (third last element in the >>> Probability array). For some reason instead of 0.18 it is showing >>> 0.1802, which I cannot figure to why. >>> >>> >>> Please see the attached code. If you see the file.txt and my output: the >>> output is not displayed in sequence and that is what I need help with. I >>> want this to 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]} >>> >>> >>> If you can figure why the output is not generated in same sequence as in >>> the file.txt for me, it will be very helpful. >>> >>> >>> let me know if I explained correctly, and if you have any questions or >>> doubts? >>> >>> >>> On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: >>> >>> 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] [
Re: [Tutor] Help needed
As others have pointed out, a mapping/dictionary or just a list of lists seems like how you would want to organize the data for input. I think your problem is insistence on using sets. I am no Python guru, but I think this list is more for exploratory learning of Python. I think people are trying to get you to answer your own questions or coax more information from you rather than simply provide their own version of your code. On Sat, Apr 26, 2014 at 3:48 PM, Suhana Vidyarthi wrote: > Thanks for the response Smith, I was thinking make be I have done > something incorrect and if there is some other function that can be used to > display the output in desired order but don't see it possible thats why was > wondering if any of you Python gurus have any inputs for me :-) > > > > On Sat, Apr 26, 2014 at 12:36 PM, C Smith wrote: > >> err, set also is unordered. I can see you are using set for a reason, but >> has no concept of order. >> >> >> On Sat, Apr 26, 2014 at 3:20 PM, C Smith wrote: >> >>> Just glancing at your work, I see you have curly braces around what >>> looks like it should be a list. If you are concerned with the order of your >>> output, dictionaries do not have a concept of order. >>> >>> >>> On Sat, Apr 26, 2014 at 3:16 PM, Suhana Vidyarthi < >>> suhanavidyar...@gmail.com> wrote: >>> Hi Danny, Let me give you a high level brief of what I am doing: I am working on doing "disaster aware routing" considering the 24-node US network where I will be setting up connection between two any two nodes (I will select the source and destination nodes randomly). Also I have some links whose "probability of failure" is mentioned in the attached file. Other links, which are not mentioned in the file - we suppose their "probability of failure" is zero. So between the source-destination nodes, there will be multiple paths and I will select the one which has "least probability of failure". Now to setup the connection between two nodes, I have to select a path whose "probability of failure" is least. To do that first I will calculate the risk of each path from the attached file and then select the path with least risk value. Did you get this part? I know it can be a bit confusing. Now I break the problem into parts: 1. I have to topology of the 24-node map 2. I have the link values of each link - where risk values are the "probability of failure" 3. I calculate the total "probability of failure" of each path (a path may have multiple links): Suppose my source node is "a" and destination node is "b". I can setup a path between a to b via c or via d (a-c-b or a-d-c): Here I will check the risk values of a-c and c-b; also risk values of a-d and d-c. If the total risk valure of a-c-b is lower that risk value of a-d-c, then I select the path a-c-d to setup the connection. (again risk value = probability of failure) Now, I will first calculate the "total probability of failure" of each link (using the file.txt) and since some links are repeated their values will be added. The probabilities get added if a link is mentioned twice or thrice. For example: link 3—5 is repeated 3 times: in line one, it has a probability of failure as 0.03, in line two it is 0.11 and in line three it is 0.04. So the probability of failure for link 3—5 is 0.03+0.11+0.04 = 0.18 The length of each array will be same. You see the code I wrote: here is the output for it: 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.18002, 0.08, 0.24] It means that link [10,13] has a "probability of failure" as [0.04] and since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and 0.04, its "probability of failure" is [0.18] (third last element in the Probability array). For some reason instead of 0.18 it is showing 0.1802, which I cannot figure to why. Please see the attached code. If you see the file.txt and my output: the output is not displayed in sequence and that is what I need help with. I want this to 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.2