user13714373
user13714373

Reputation:

Nested For Loops in Python (calculating the perimeters of triangles)

I want to calculate perimeters of triangles. I want to access concrete elements of the list, so I am using a nested for loop. 1) I would like to know why the position of perimeter makes my code different in both variants. (in the second one I don't understand why the output is 240) 2) I would like to know how to concatenate all items in the nested lists, so the output should be like that: (I tried using indexing, but it didn't work)

60
150
240

The First:

def function(triangle):
    perimeter = 0
    for i in triangle:
        for j in i:
            perimeter = perimeter + j
    return perimeter

function([[10,20,30], [40,50,60], [70,80,90]])
450

The Second:

def function(triangle):
    for i in triangle:
        perimeter = 0
        for j in i:
            perimeter = perimeter + j
    return perimeter

function([[10,20,30], [40,50,60], [70,80,90]])
240

Upvotes: 1

Views: 327

Answers (4)

Vignesh
Vignesh

Reputation: 1623

Minor fix for return all perimeter as list

def function(triangle):
    perimeter_list = []
    for i in triangle:
        perimeter = 0
        for j in i:
            perimeter = perimeter + j
        perimeter_list.append(perimeter)
    return perimeter_list

function([[10,20,30], [40,50,60], [70,80,90]])

Output

[60, 150, 240]

Upvotes: 0

Tony Suffolk 66
Tony Suffolk 66

Reputation: 9704

In your first example - since you set perimeter to be zero before you start looping through any of the triangles, you calculate the total perimieter of all of the listed triangles.

By moving the perimeter = 0 inside the outer loop (in your 2nd example), you are discarding the total for the first triangle after you calculate it, and then you are discarding the total for the 2nd triangle before you calculate it again for the third.

I would re-write your code like this :

def perimeters(geom):
    for triangle in geom:
         yield sum(triangle)

you now have a generator which you can loop around - or convert to a list, or pass to a function such as map, filter etc.

for perimeter in perimeters([[10,20,30], [40,50,60], [70,80,90]])
    print(perimeter)

The Benefit of using generators is that it calculates the data when you need it, rather than doing all the caulculations up front.

Upvotes: 0

quamrana
quamrana

Reputation: 39354

It looks like you want to return the perimeters of each triangle supplied to your function.

This can be expressed almost directly in python:

def get_perimeters(triangles):
    return [sum(perimeters) for perimeters in triangles]

print(get_perimeters([[10,20,30], [40,50,60], [70,80,90]]))

Output:

[60, 150, 240]

Upvotes: 0

Koralp Catalsakal
Koralp Catalsakal

Reputation: 1124

1 - The difference between two codes is that, in the first one perimeter is set to 0 before the loop starts, and than updated throughout the loop. In the second one, you set the perimeter value to 0 every time the outer loop is executed, so the value from previous loop is not accumulated in it.

2- For loop you can use the following method:

triangles = [[10,20,30], [40,50,60], [70,80,90]]
perimeters = list(map(sum,triangles))
print(perimeters)
[60, 150, 240]

Here, map iterates over the elements of triangles and calls the sum function for each element. Then, using list you can convert it to a list of perimeter values.

Upvotes: 3

Related Questions