0

considering the following code is there a function that take the odd indexed values and put it in array and the even to put it in another array then relate two arrays elements to each other in order to do some processing on it.

hm to run: ") if IN == '1':

 PN = input("   Enter each process time following by its arrival time separated by comma:")
        Ptimes = [] 
        Atimes = []
3
  • 1
    How is the code you have posted related to your question, if at all? Commented Apr 17, 2014 at 9:56
  • it's a part of the code that i am asking about sorry i will delete the extra useless code thanks for your note Commented Apr 17, 2014 at 9:59
  • What does hm to run: ") mean? Commented Apr 17, 2014 at 10:09

2 Answers 2

1

If you are used to zero-based arrays (index 0, index 1):

even = data[::2]  # even indices: 0, 2, 4, ...
odds = data[1::2]  # odd indices: 1, 3, 5, ...

If prefer to think in one-based arrays (1st element, 2nd element):

odds = data[::2]  # 1st element, 3rd element, ...
even = data[1::2]  # 2nd element, 4th element, ...
Sign up to request clarification or add additional context in comments.

Comments

1

You can get the values at even indices like this:

PN[::2]  # Start at 0, jump every two so 0, 2 etc.

and the ones at odd like this:

PN[1::2]  # Start at 1, jump every two so 1, 3 etc.

7 Comments

does comma considered as an array element i mean i want to pass the comma and ignore it consider it an integer array or do i have to tell the user to separate numbers by a space instead of comma?
@user3541982 I would just do it with spaces, and then just split() the input to get a list out of it.
@AlexThornton that gets the items at even/odd indices.
@jonrsharpe I don't understand, he said in the question he wanted to 'get the odd-indexed values'.
@AlexThornton yes, that is what you give, but your description is "get the even indices"
|

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.