Select to view content in your preferred language

Move to next item in list

1068
6
Jump to solution
05-13-2014 08:32 AM
NoahHuntington
Deactivated User
I am attempting to utilize a function to save the output of a geoproceessing tool inside a loop.  The function iterates thru the list with each iteration of the loop.   However if I want to save the output feature class as function " + 1" how do I achieve this. This would be similar to function(count + 1)? Only using characters instead of integers.

Function:
def get_LCP(letter):     for letter in ['A','B','C','D','E','F']:         return os.path.join(savepath  + "\\Scratch.gdb"  + "\\LCP_" + (letter))


Loop:
    count = 0     while count < 2: ...         costPath2 =CostPath(originValue, setNull2,get_backlink(count + 1),"EACH_CELL","")         costPath2.save(get_LCP(letter).next) 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Zeke
by
Honored Contributor
I think what you want to do is change 'get_backlink(count + 1)' to 'get_backlink(chr(count + 64)) ', or possibly to 65 0r 66, can't be sure from your code, since it looks like you first use count when it's 1. Depends on the rest of your code. Since you're first looking for 'A', you want to add whichever number gets you to 65, assuming this is ascii.

However, you'll never get past 'C' , since your loop exits once it hits 2.

edit: oh, and don't forget to increment count or you'll be in an infinite loop. Maybe that's elsewhere in your code.

View solution in original post

0 Kudos
6 Replies
MattEiben
Deactivated User
If I understand you correctly, it looks like you're looking to save the same output 6 times for each iteration of your while loop.  If so, than maybe something like this would work for you.

def get_LCP(costPath2):
    for letter in ['A','B','C','D','E','F']:
        path = os.path.join(savepath  + "\\Scratch.gdb"  + "\\LCP_" + letter)
        costPath2.save(path)

count = 0
while count < 2:
    .....
    costPath2 = CostPath(originValue, setNull2,get_backlink(count + 1),"EACH_CELL","")
    get_LCP(costPath2)
0 Kudos
NoahHuntington
Deactivated User
Hi Matt,

Thanks for the reply. I probably added to confusion having 6 items in the list.  What I am looking for is replacing the count index with the equivalent letter.  A instead of 0, B instead of 1, so on and so forth... I hope this helps?
0 Kudos
Zeke
by
Honored Contributor
You may want to look at chr(). This will return the string version of the numeric value, for example, chr(97) = 'a'. You'd have to look up the value of the character in your encoding and add that to your counter. For example, in ascii, A = 65, B = 66, C = 67...a = 97, b = 98...etc. So in ascii, if your counter starts at 0, add 65 to it so that 0 will become A, 1 will become B, etc. Unicode and other encodings are probably different.
0 Kudos
NoahHuntington
Deactivated User
You may want to look at chr(). This will return the string version of the numeric value, for example, chr(97) = 'a'. You'd have to look up the value of the character in your encoding and add that to your counter. For example, in ascii, A = 65, B = 66, C = 67...a = 97, b = 98...etc. So in ascii, if your counter starts at 0, add 65 to it so that 0 will become A, 1 will become B, etc. Unicode and other encodings are probably different.


Thanks Greg!  I am not familiar with these properties.  I will spend a little time with the literature and see if I can get something to work.

-Noah
0 Kudos
Zeke
by
Honored Contributor
I think what you want to do is change 'get_backlink(count + 1)' to 'get_backlink(chr(count + 64)) ', or possibly to 65 0r 66, can't be sure from your code, since it looks like you first use count when it's 1. Depends on the rest of your code. Since you're first looking for 'A', you want to add whichever number gets you to 65, assuming this is ascii.

However, you'll never get past 'C' , since your loop exits once it hits 2.

edit: oh, and don't forget to increment count or you'll be in an infinite loop. Maybe that's elsewhere in your code.
0 Kudos
NoahHuntington
Deactivated User
I believe this method is going to work for me.  I will get this syntax in the right places.  Thanks Greg!
0 Kudos