This article mainly describes the python for the given list of elements of the method of flipping, combined with an instance of the form of Python for the list of elements based on slicing and traversal output of two flip operation implementation skills, with a certain reference value, the need for friends can refer to the next
This example describes how Python flips the elements in a given list. Share to everyone for your reference, as follows:
Topic
Given a list, flip the elements, reverse the output
The approach is simple, here are two approaches, the first is the simplest to use for the list of slicing operations, the following is a concrete implementation
#!usr/bin/env python#encoding:utf-8 "' __author__: Yishui Cold City Features: Flip list ' Def inverse_list1 (num_list): ' ' flip list ' print num_list[::-1]def inverse_list2 (num_list): ' ' flip list ' ' n = len (num_list) For i in Xrange (N/2): t = num_list[i] num_list[i] = num_list[n-1-i] num_list[n-1-i] = t print n Um_listif __name__ = = ' __main__ ': print "script home test Result:" num_list=[1,2,3,4,5,6,7,8,9,0] inverse_list1 ( num_list) Inverse_list2 (num_list)
The results are as follows:
Script Home Test results:
[0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
The results of the operation are as follows:
The simplest way to flip is to see the slice-based operation from the above example comparison.