Python method analysis for flipping operations on elements in a given list

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.