Python Forum
Fill as list (with conditions).
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fill as list (with conditions).
#1
Hello ! =)

I'm new is Python. I would like create a function which is responsible for filling a list with 0, 1, -1 values depending on 4 other list values. I wrote a code, but it doesn't work, I don't understand why. Any of your help is welcome. Thank you!

# Position function

def Startegy(A, B, C, D):

# Create a vector of positions

    position = []
    position.append(0)

# If previous position is 0
    for i in A:
        if position(i-1) == 0:
            if A(i) > B(i) > C(i) > D(i):
                position(i) == 1
            if A(i) < B(i) < C(i) < D(i):
                position(i) ==-1
# If previous position is -1
        elif position(i-1) == -1:
            if A(i) > B(i) > C(i) > D(i):
                position(i) == 1
            if A(i) < B(i) < C(i) < D(i):
                position(i) == -1
# If previous position is 1
        elif position(i-1) == 1:
            if A(i) > B(i) > C(i) > D(i):
                position(i) == 1
            if A(i) < B(i) < C(i) < D(i):
                position(i) == -1
# In other case keep the position
        else:
            position(i) == position(i-1)
    
    return position

A = [1,5,6,9,10,15]
B = [1,4,5,8,12,10]
C = [1,3,4,7,8,6]
D = [1,2,3,4,5,5]

res = Startegy(A,B,C,D)

print(res)
Reply
#2
Any errors? Doesn't work is not much descriptive.

The list's index is closed between [] not ().
So in your case position() is expected to be a function call. Same for A() if A is a list.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Thank you. I've replaced all "()" by "[]". Now the error message desapeared. Unfortunately it return me "[0]", which mean that the list is not filling...
Does anyone have an idea why?

Thank you.

# Position function

def Startegy(A, B, C, D):

# Create a vector of positions

    position = []
    position.append(0)

# If previous position is 0
    for i in position:
        if position[i-1] == 0:
            if A[i] > B[i] > C[i] > D[i]:
                position.append(1)
            if A[i] < B[i] < C[i] < D[i]:
                position.append(-1)
# If previous position is -1
        elif position[i-1] == -1:
            if A[i] > B[i] > C[i] > D[i]:
                position.append(1)
            if A[i] < B[i] < C[i] < D[i]:
                position.append(-1)
# If previous position is 1
        elif position[i-1] == 1:
            if A[i] > B[i] > C[i] > D[i]:
                position.append(1)
            if A[i] < B[i] < C[i] < D[i]:
                position.append(-1)
# In other case keep the position
        else:
            position[i] == position[i-1]

    return position

A = [1,5,6,9,10,15]
B = [1,4,5,8,12,10]
C = [1,3,4,7,8,6]
D = [1,2,3,4,5,5]

res = Startegy(A,B,C,D)

print(res)
Reply
#4
You are modifying the list on the go during the for loop. This is not a good idea. I admit that I am unable to see what is going on looking at the script.

If you want to play with the index you can use enumerate.
for index, element in enumerate([4,3,2,1,0]):
    print(index + 1, element)
Output:
1 4 2 3 3 2 4 1 5 0
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Thank you, But what I want to do is to fill the list "posistion" with "0","1" or "-1" values depending on values from whether lists "A < B < C < D" or lists "A > B > C > D", and depending on previous (i-1) values of the list "position".
Reply
#6
there are number of problems in your code.
1. in your code, i is the item in the list positions (line 11). At the same time, in the following code you treat it as index. I would assume it's not what you want. So you check that positions[i-1] == 0 (that is positions[-1]==0) and want to check that A[i] > B[i] > C[i] > D[i] or that A[i] < B[i] < C[i] < D[i] in order to append -1. however A[0] == B[0] == C[0] == D[0] == 1 and you don't add anything. so it exits the loop because there are no more values to iterate over
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(May-11-2018, 01:41 PM)EmericCrue Wrote:
    position = []
    position.append(0)
 
# If previous position is 0
    for i in position:
position has one element (it's 0), so your loop will only ever run once. Is that your intention?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020