0

I have a list of code to update the value of time for each 5 elements. This is the code:

msg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
theta=1
time=4.5

def get_new_theta(msg, theta,time):
    new_theta = [theta]
    new_time = [time]
    for a, b in zip(msg[3::5], msg[4::5]):
        new_theta.append(new_theta[-1] + a + b)
        new_time.append(new_time[-1]+time)
    return new_theta
    return new_time

for i, theta in enumerate(get_new_theta(msg, theta,time)[:-1]):
    for j in range(5):
        print(theta, msg[i*5+j],time)

However, I did not get the desired output. The desired output should be:

1 1 4.5
1 2 4.5
1 3 4.5
1 4 4.5
1 5 4.5
10 6 9
10 7 9
10 8 9
10 9 9
10 10 9
29 11 13.5
29 12 13.5
29 13 13.5
29 14 13.5
29 15 13.5

Need help from this forum. Thank you.

3
  • 2
    You have two return statements but only one will ever be reached. Commented Aug 21, 2018 at 6:41
  • 2
    Use return new_theta, new_time Commented Aug 21, 2018 at 6:44
  • @ParthS007. I have try it but it is not same as the desired output. Commented Aug 21, 2018 at 6:52

1 Answer 1

1
def get_new_theta(msg, theta, time):
    new_theta = [theta]
    new_time = [time]
    for a, b in zip(msg[3::5], msg[4::5]):
        new_theta.append(new_theta[-1] + a + b)
        new_time.append(new_time[-1]+time)
    return new_theta[:-1], new_time[:-1]

msg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
theta = 1
time = 4.5

for i, (theta, time) in enumerate( zip(*get_new_theta(msg, theta, time)) ):
    for j in range(5):
        print(theta, msg[i*5+j], time)

gives:

1 1 4.5
1 2 4.5
1 3 4.5
1 4 4.5
1 5 4.5
10 6 9.0
10 7 9.0
10 8 9.0
10 9 9.0
10 10 9.0
29 11 13.5
29 12 13.5
29 13 13.5
29 14 13.5
29 15 13.5

get_new_theta(msg, theta, time) returns a tuple of the lists (new_theta, new_time). And zip(*get_new_theta(msg, theta, time)) gives an iterator over tuples (theta, time)

Sign up to request clarification or add additional context in comments.

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.