3

I'm trying to use a for loop in a list to assign different buffer widths to the list depending on the length of the line being buffered. I have my list of lengths, and tried to use a for loop to iterate the list with an output variable to hold the new buffer width values. When I say print buffer_width, the result is "300 Feet" so the for loop is not going though every item in the list, just printing the last item. Finally, I'd like to place my buffer_width variable into the buffer_analysis() function.

length = [1990, 2016, 3051, 3279, 1855, 3432, 4323, 3016, 4218]

for le in length:
    if le < 3000:
        buffer_width = "75 Feet"
    elif le > 4000:
        buffer_width = "300 Feet"
    else:
        buffer_width = "175 Feet"

print buffer_width

closed as off-topic by Dan C, Michael Stimson, PolyGeo Mar 2 '17 at 6:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Welcome to GIS SE! What GIS software are you using? What are you buffering, as there's no reference to shapefile/layer etc. in your code snippet – Midavalo Mar 1 '17 at 17:54
  • 1
    I think your understanding of a for loop is fundamentally flawed, you will only get the last item in the list because the loop doesn't exit until the last value is reached, the print statement is only encountered once. If you tab (or space) it into the loop it will print each value in turn. From the sounds of it you want to calculate the value into a field and then buffer using this field, is this correct or is the buffer value for a whole feature class? At the moment it's a bit too ambiguous as to what you're trying to do here with your values. – Michael Stimson Mar 2 '17 at 4:30
5

Your code is correct, your indent is misplaced. Try this:

length = [1990, 2016, 3051, 3279, 1855, 3432, 4323, 3016, 4218]

for le in length:
    if le < 3000:
       buffer_width = "75 Feet"
    elif le > 4000:
         buffer_width = "300 Feet"
    else:
         buffer_width = "175 Feet"
    print le,buffer_width
3

Your buffer_width being a variable is getting changed on every iteration of your loop. If you want the the value of buffer width for each element in length, you'll need to define buffer_width as a list.

Try-

length = [1990, 2016, 3051, 3279, 1855, 3432, 4323, 3016, 4218]
buffer_width = []
for le in length:
    if le < 3000:
        buffer_width.append("75 Feet")
    elif le > 4000:
        buffer_width.append("300 Feet")
    else:
        buffer_width.append("175 Feet")

for i in buffer_width:
    print i
1

I think nash is on to something. Maybe if we assume you want to convert the original array length of number into an array of strings

length = [1990, 2016, 3051 ... ]

becomes

length = ["75 feet", "75 feet", "175 feet" ...]

Try this -

length = [1990, 2016, 3051, 3279, 1855, 3432, 4323, 3016, 4218]

buffer_width = []
for le in length:
    if le < 3000:
        buffer_width.append("75 Feet")
    elif le > 4000:
        buffer_width.append("300 Feet")
    else:
        buffer_width.append("175 Feet")

length = buffer_width

Also length is not a good name for a list since a list can have a length, maybe buffer_length is a better name.

Not the answer you're looking for? Browse other questions tagged or ask your own question.