-2

Sample data (towers1.txt):

MS33 42.19 -70.33 3.6
JS89 42.23 -70.30 3.9
ED22 42.25 -70.33 3.4
HE90 42.27 -70.35 3.7
TW05 42.30 -70.30 3.4
WW23 42.37 -70.28 3.7

Code:

lr = []

import lat_long_to_dist
f = open('towers1.txt','r')   
towers = [line.strip('\n') for line in f]
for line in towers:
     lr.append(line.split())

I'm trying to figure out how to use 2 for loops only (I have not learned dictionaries and above) to have the first for loop for the Towers themselves, then a second for loop for all the towers except the ones chosen in the first loop. While only using the formula below from file lat.py I have (this cannot be edited) ie. a line in my code must read

lat.distance_from_lat_long( lat1, long1, lat2, long2 )

Thus I need from the first for loop a lat1 and long1 which will stay the same until they read the lat2 and long2 of every other tower in the second for loop, then the 2nd tower goes to the first for loop and so on, and that will give me the distance between 2 of the towers.

A tower can work (count=+1) if from the nested for loop, the minimum of the lr[3] from lines is smaller than the 2 towers distance, then to return the name of the tower in the first for loop and which other towers it can work with, same for the 2nd, and which one that can and so on. Just a bit confused; wondering if anyone can help?

I'm not sure if this helps but I've been trying for hours and can't come up with anything. Here's my code right now and it's so bad maybe someone can help?

def Tower_Dist_From_Eachother(t,count):  
    for i in range(count):
        for j in range(count):
            lats = 0
            longs = 0              
            tdist = lat.distance_from_lat_long(lats, longs, float(lr[j][1]), float(lr[j][2]))

def towers(l):
    towname = []
    for i in range(count):
        towname.append(at[i][0])

for i in range(count):
    lats += float(lr[i][1])
    longs += float(lr[i][2])
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • For your new code, what does "it's so bad" mean? What do you expect it to do, and what does it do instead? – abarnert Aug 05 '13 at 23:15
  • in the instructions i wrote a paragraph on what i want the code to do. – user2605035 Aug 05 '13 at 23:19
  • Your explanation of what you want to do is a little clearer than mud, but not by much. You need to rework the question to explain much more clearly what you are after. My answer calculated the distance between each pair of towers in your list, which seemed to be what you were after. But you've stipulated that calculating the distance between each pair of towers is not what you're after (since you demanded I delete my answer), so you must really work very hard to restate what you do want. – Jonathan Leffler Aug 05 '13 at 23:37
  • with my list of towers1.txt, say it =f, i need to take the f[0][1] and f[0][2] of the first tower and use the (lat1,long1,lat2,long2) with the rest of the lists below f[x][1], f[x][2] then take f[1][1] f[1][2], with the rest of list, and so on – user2605035 Aug 05 '13 at 23:43
  • i posted another question which is simplified @jonathanleffler – user2605035 Aug 06 '13 at 00:01
  • possible duplicate of [SIMPLIFIED: Create a nested for loop for the data and plug the "4" values i get EACH time into a function i already have defined](http://stackoverflow.com/questions/18069755/simplified-create-a-nested-for-loop-for-the-data-and-plug-the-4-values-i-get) – Hyperboreus Aug 06 '13 at 00:09
  • @user2605035: Stop creating new questions. Answer the questions people ask you. Explain what needs to be explained in comments, and/or edit your question to make sense. Otherwise, someone will have to read all of your different question, and the (repetitive) string of rejected answers and half-responded-to-comments, before they can even begin to try to help you. – abarnert Aug 06 '13 at 00:29

1 Answers1

1

I'm assuming this part is the real problem:

So Im trying to figure out how to use 2 for loops only (I have not learned dictionaries and above) to have the first for loop for the Towers themselves, then a second for loop for all the towers except the ones chosen in the first loop.

So you want to loop over all of the towers except i, right? The easy way to do that is to loop over all of the towers, and just skip i. For example:

for i in towers:
    for j in towers:
        if j == i:
            continue
        # do stuff with i and j

Or, equivalently:

for i in towers:
    for j in towers:
        if j != i:
            # do stuff with i and j
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I want to 2nd for loop over the lr[1] and lr[2] values of all the towers not in the first loop, and those values will go in lat1 lat2 long1 long2 – user2605035 Aug 05 '13 at 22:56
  • what is i in your comment?? – user2605035 Aug 05 '13 at 23:06
  • @user2605035: I don't have a comment anywhere, and I don't see any comments by anyone other than you, so… what are you referring to? In my code, `i` is each tower in turn. And then, for each `i`, `j` is again each tower in turn, but I'm skipping over the case where `j` is the same tower as `i`. I think that's what you were asking for. But that's really just a guess, because your question is very confusing. – abarnert Aug 05 '13 at 23:14
  • posted another version of the question – user2605035 Aug 06 '13 at 00:04