python - 在python中根据索引及其后面的项目切片列表

我有一系列度数值,如下所示:


DEGREES = [


 0, 15, 30, 45, 60, 


 75, 90, 105, 120,


 135, 150, 165, 180,


 195, 210, 225, 240,


 255, 270, 285, 300,


 315, 330, 345,


]



我如何选择一个特定的值,比如90,然后能够找到后面的12个元素,包括最后的索引?


[90, 75, 60, 45, 30, 15, 0, 345, 330, 315, 300, 285, 270]



使用切片表示法,我尝试了这样做:


index = DEGREES.index(90)


print(DEGREES[index-12:index]) # start 12 values back, stop at index



但这只打印出一个空的数组。

有没有一种方法可以对一个列表进行切片,这样,我就可以在我使用的索引后面得到前面的12个值。

时间:

或者你可以用一个deque


from collections import deque



dq = deque(reversed((0, 15, 30, 45, 60,


 75, 90, 105, 120,


 135, 150, 165, 180,


 195, 210, 225, 240,


 255, 270, 285, 300,


 315, 330, 345)))



index = dq.index(90)


dq.rotate(-index)


res = list(dq)[:13]


# [90, 75, 60, 45, 30, 15, 0, 345, 330, 315, 300, 285, 270]



像这样的东西可能更直接:


index = DEGREES.index(90)


print([DEGREES[i] for i in range(index, index-12, -1)])



你需要的是一个处理此异常的语句:


index = DEGREES.index(90)


if index >= 12:


 print(DEGREES[index-12:index])


else: 


 print(DEGREES[:index])



这里有一种使用按键排序的方法,它假设列表已经排序,就像在共享示例中一样:


l = [


 0, 15, 30, 45, 60, 


 75, 90, 105, 120,


 135, 150, 165, 180,


 195, 210, 225, 240,


 255, 270, 285, 300,


 315, 330, 345,


]



sorted(l, key = lambda x: (x <= 90, x), reverse=True)[:13]


[90, 75, 60, 45, 30, 15, 0, 345, 330, 315, 300, 285, 270]



关于第一种方法是基于以下tuples进行排序:


t = [(x <= 90, x) for x in l]



[(True, 0),


 (True, 15),


 (True, 30),


 (True, 45),


 (True, 60),


 (True, 75),


 (True, 90),


 (False, 105),


 (False, 120),


 (False, 135),


 (False, 150),


 (False, 165)]


 ...



在排序时,生成预期输出:


sorted(t, reverse=True)[:13]



[(True, 90),


 (True, 75),


 (True, 60),


 (True, 45),


 (True, 30),


 (True, 15),


 (True, 0),


 (False, 345),


 (False, 330),


 (False, 315),


 (False, 300),


 (False, 285),


 (False, 270)]



按列表切片


DEGREES = [


 0, 15, 30, 45, 60, 


 75, 90, 105, 120,


 135, 150, 165, 180,


 195, 210, 225, 240,


 255, 270, 285, 300,


 315, 330, 345,


]



value = 90


index = DEGREES.index(value)



result = DEGREES[:index+1][::-1] + DEGREES[index+1:][::-1]


result = result[:13]


print(result)



输出


[90, 75, 60, 45, 30, 15, 0, 345, 330,


 315, 300, 285, 270]



不幸的是,你不能这么做,你可以连接这些Fragment,这可能有点麻烦:


DEGREES = [


 0, 15, 30, 45, 60, 


 75, 90, 105, 120,


 135, 150, 165, 180,


 195, 210, 225, 240,


 255, 270, 285, 300,


 315, 330, 345,


]



index = DEGREES.index(90)


result = DEGREES[index - 11:index + 1] if index >= 11 else (DEGREES[index - 11:] + DEGREES[:index + 1])


print(result)


# [285, 300, 315, 330, 345, 0, 15, 30, 45, 60, 75, 90]



或者使用列表推导:


DEGREES = [


 0, 15, 30, 45, 60, 


 75, 90, 105, 120,


 135, 150, 165, 180,


 195, 210, 225, 240,


 255, 270, 285, 300,


 315, 330, 345,


]



index = DEGREES.index(90)


result = [DEGREES[i] for i in range(index - 11, index + 1)]


print(result)


# [285, 300, 315, 330, 345, 0, 15, 30, 45, 60, 75, 90]



我想你需要做一些算术运算。


index = DEGREES.index(90) + 1


offset = 12


start = index - offset


length = len(DEGREES)


print(


 list(reversed(DEGREES[max(0, start):index])) + 


 (list(reversed(DEGREES[length + start - 1 :length])))


 if start < 0


 else [])


)



或者:


DEGREES = [


 0, 15, 30, 45, 60, 


 75, 90, 105, 120,


 135, 150, 165, 180,


 195, 210, 225, 240,


 255, 270, 285, 300,


 315, 330, 345,


]



index = DEGREES.index(90)



subFront = DEGREES[:index + 1][-12:]


subFront.reverse()



remainLen = 12 - len(subFront) + 1


if remainLen > 0:


 subBack = DEGREES[-remainLen:]


 subBack.reverse()


 subFront = subFront + subBack


print(subFront)


[90, 75, 60, 45, 30, 15, 0, 345, 330, 315, 300, 285, 270]



基于itertools的解决方案:


from itertools import cycle, islice



DEGREES = [


 0, 15, 30, 45, 60,


 75, 90, 105, 120,


 135, 150, 165, 180,


 195, 210, 225, 240,


 255, 270, 285, 300,


 315, 330, 345,


]



gen = cycle(reversed(DEGREES))



# find next 90


next(item for item in gen if item == 90)


res = [90] + list(islice(gen, 12))


# [90, 75, 60, 45, 30, 15, 0, 345, 330, 315, 300, 285, 270]



我建议你尝试itertools.cycle(),以获得任何以前的值。

只需反转列表,并且尝试cycle()


import itertools



degrees = [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345]


n=12


degrees.reverse()


ind = degrees.index(90)


degrees = degrees[ind:]+degrees[:ind]


rev_cycle = itertools.cycle(degrees)


for i in range(n+1):


 print(next(rev_cycle))



这是有效的,因为它使用生成器。

...