4

I am new to python and I want to build a function that updates part of a list according to a condition.

here is an example of what I want:

List1=[1,2,3,4,10,5,9,3,4]
List2=[2,4,6,8]

I want to update List1 to be [2,4,6,8,10,5,9,6,8], and here is my code to do that:

x = [1, 2, 3, 4, 10, 5, 9, 3, 4]
y = [2, 4, 6, 8]


def update_signal(gain):
    for i in range(0, len(y)):
        for j in range(0, len(x)):
            if x[j] == y[i] / gain:
                x[j] = y[i]
            elif x[j - 1] == y[i] / gain:
                break


update_signal(2)  # for this example only gain =2
print("x=", x)
print("y=", y)

the expected output is:

x=[2,4,6,8,10,5,9,6,8] 
y=[2,4,6,8]

what it actually prints is:

x= [8, 8, 6, 8, 10, 5, 9, 6, 8]
y= [2, 4, 6, 8]

so, what am I doing wrong to make this function behave like this?

5
  • "according to a condition" -- what is the condition? Commented Mar 12, 2020 at 6:59
  • You're processing x multiple times. So after you replace an element, you'll consider the new element when checking the condition against a different element of y. Commented Mar 12, 2020 at 7:00
  • Maybe you should change the order of the loops, and make x the outer loop. Commented Mar 12, 2020 at 7:03
  • And break out of the inner loop whenever you perform a replacement. Commented Mar 12, 2020 at 7:03
  • Can you explain the expected logic(condition) in sentence? Commented Mar 12, 2020 at 7:09

4 Answers 4

1

Maybe something like this?

def update_signal(gain):
    return [item * gain if item * gain in y else item for item in x]
Sign up to request clarification or add additional context in comments.

2 Comments

It may different if y's elements are not divided by gain. For example, y = [1, 3, 5] and gain = 2.
This is maybe not OP expected.
1

I assume that your algorithm means:

  • let i is x's arbitrary element.
  • let j is y's arbitrary element.
  • if i is same with j / gain, replace i to j

(If I am wrong, point it please. Then I'll fix for it.)

x = [1, 2, 3, 4, 10, 5, 9, 3, 4]
y = [2, 4, 6, 8]


def update_signal(gain):
    convert_table = {i / gain: i for i in y}
    x[:] = [convert_table.get(i, i) for i in x]


update_signal(2)  # for this example only gain =2
print('x = ', x)
print('y = ', y)

output:

x =  [2, 4, 6, 8, 10, 5, 9, 6, 8]
y =  [2, 4, 6, 8]

Comments

1

Please try the code below:

x = [1, 2, 3, 4, 10, 5, 9, 3, 4]
x_copy = x[:]
y = [2, 4, 6, 8]

def update_signal(gain):
    for i in range(0, len(y)):
        cond = y[i] / gain
        for j in range(0, len(x)):
            if x[j] == cond:
                x_copy[j] = y[i]
            elif x[j - 1] == cond:
                continue


update_signal(2)  # for this example only gain =2
print("x=", x_copy)
print("y=", y)

3 Comments

Your continue statement isn't needed since it's the last line of code in the loop anyway ;-) it looks like this one works though @Marcus
@Todd Yes, you're right. But since I still don't understand what the asker means of the condition judgment elif x[j - 1] == cond:, I keep it in my answer.
The results are the same whether to declare globla or not. list is global variable in Python. So there is no need to declare global x_copy here since x_copy is a list.
1
x=[1,2,3,4,10,5,9,3,4]
y=[2,4,6,8]
def update_signal(gain):

    for i in range(len(x)):
        for j in range(len(y)):
            if y[j]//x[i]==gain:
                x[i]=y[j]
                break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.