문제

so i have a tuple in python and i need to slice from the last element to first element but only want the last and first...

example:

big tuple: (1,2,3,4,5)

i want to slice the tuple so i end up with (5,1)

some_list[::len(some_list)-1]

above code slices from first to last and only includes the first and last..taken from here! so i need the exact opposite.

도움이 되었습니까?

해결책

This code works:

some_list[-1:-len(some_list)-1:-(len(some_list) - 1)]

Another way:

some_list[::-len(some_list) + 1]

Of course you always can do

x[::len(x) - 1][::-1]

다른 팁

Why not just do

new_list = (some_list[-1], some_list[0])

?

a = (1,2,3,4,5)    
print(a[0],a[-1])

Please try this. It has worked for me and seems to be a simple solution.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top