简体   繁体   中英

Indexing specific values in a list to be changed based off a corresponding index

I have two arrays from previous functions

ALPt = (90, -70, 90)
N = [1, 2, 1]

I've defined a new function to do operations with each element in N but I want to set a condition based on the sign of the corresponding element in ALPt I've tried a few different methods, this is the closest I have

def ALPe(): 
    ALPe = []
    for i in N:
        ALPe0 = math.degrees(math.acos((i * Bt) / (math.pi * Dk)))
        ALPe.append(ALPe0)
    for n, j in enumerate(ALPe):
        for m, k in enumerate(ALPt):
            if k < 0:
                ALPe[n] = j * -1
    return ALPe

ALPe = ALPe()
print(ALPe)

Output:

[-80.8421504880813, -71.43925528310385, -80.8421504880813]

I am missing something that changes the sign of the n element in ALPe based on m element in ALPt, which would make the output look like this:

[80.8421504880813, -71.43925528310385, 80.8421504880813]

any suggestions?

just looking at:

So each element in ALPe should have the same sign as the element in ALPt at the same position? – glibdud 1 hour ago

that is the goal – chic9009

is this what is wanted?

a = [1.0, -2.0, -3.0, 4]
b = [4, -3, 2 , -1.0]

[abs(a)*((-1)**(b < 0)) for a, b in zip(a,b)]

Out[41]: [1.0, -2.0, 3.0, -4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM