0

Estou tentando fazer o procedimento abaixo porém x e y são uma lista de números e por isso esta dando erro. Usei o seguinte código:

theta = [180* math.atan(y/x)/math.pi for x,y in zip(media_v1, media_v2)] 

if x > 0 and y <0:
    theta = theta + 90
elif x < 0 and v < 0:
    theta = theta + 180
elif x < 0 and v > 0:
    theta = theta + 270

O primeiro eu consegui fazer o procedimento com a lista (media_v1 e media_v2) toda usando o 'for', porém quando tento fazer o 'if' ele não faz para a lista toda e dá o seguinte erro:

TypeError: '>' not supported between instances of 'list' and 'int'

0

2 Respostas 2

0

Acredito que o list comprehension não irá te ajudar nisso, por causa do if. Use uma estrutura normal de for

theta_list = []

for x,y in zip(media_v1, media_v2):
    theta = 180 * math.atan(y/x)/math.pi
    if x > 0 and y <0:
        theta = theta + 90
    elif x < 0 and y < 0:
        theta = theta + 180
    elif x < 0 and y > 0:
        theta = theta + 270

    theta_list.append(theta)

Depois use o valor dos theta dentro da lista iterando. Por exemplo:

for t in theta_list:
    print(t)
0

Você pode separar a lógica em uma função e aplicar dentro da list comprehension visto que o seu IF se aplica a cada iteração e não ao resultado final:

import math


def calculate_theta(x, y):
    t = 180 * math.atan(y/x) / math.pi
    if x > 0 and y <0:
        return t + 90
    elif x < 0 and y < 0:
        return t + 180
    elif x < 0 and y > 0:
        return t + 270
    else:
        return t


media_v1 = [1, 2, 3, 4, 5]
media_v2 = [4, 1, 5, 2, 3]


theta = [calculate_theta(x, y) for x, y in zip(media_v1, media_v2)]

Código executando Repl.it

Você deve fazer log-in para responder a esta pergunta.