1) List reverse order
A, List.reverse (): Reverse the list;
L = [1, 2, 3, 4, 5]
Print (L.reverse ())
-->[5, 4, 3, 2, 1]
B, L.[::-1]--[5, 4, 3, 2, 1]
# L.[:-1]--[1, 2, 3, 4]
C, Reversed (list): Get the list of the reverse iterator;
Available: for x in Reversed (list): To reverse iterate the list;
# when executing reversed (list), call the __reversed__ () method, which is the inverse iterative interface;
# liter (list): Gets the list's forward iterator;
classFloatrange:def __init__(self, start, end, step = 0.1): Self.start=Start Self.end=End Self.step=Stepdef __reversed__(self): t=Self.end whileT >=Self.start:yieldT T-=Self.stepdef __iter__(self): t=Self.start whileT <=Self.end:yieldT T+=Self.step#this loop, when instantiated, automatically calls the __iter__ () method instead of the __reversed__ () method; forXinchFloatrange (1.0, 3.0, 0.5): Print(x)#this cycle, only after defining the __reversed__ () method, can use the reversed directly; forXinchReversed (Floatrange (1.0, 4.0, 0.5)): Print(x)
Python: List of lists