这个函数是用Python内置的吗?

时间:2014-04-17 09:52:01

标签: python function methods

考虑下面的代码是有一个函数,它采用奇数索引值并将其放在数组中,偶数将它放在另一个数组中然后将两个数组元素相互关联,以便对它进行一些处理。

要运行:")         如果IN ==' 1':

 PN = input("   Enter each process time following by its arrival time separated by comma:")
        Ptimes = [] 
        Atimes = []

2 个答案:

答案 0 :(得分:1)

您可以在偶数索引中获取值:

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

和奇怪的人一样:

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

答案 1 :(得分:1)

如果您习惯于从零开始的数组(索引0,索引1):

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

如果想考虑基于一个数组(第一个元素,第二个元素):

odds = data[::2]  # 1st element, 3rd element, ...
even = data[1::2]  # 2nd element, 4th element, ...