Python sequence and Python Sequence

Source: Internet
Author: User

Python sequence and Python Sequence
Python contains six built-in sequences: List, tuples, strings, Unicode, buffer objects, and xrange objects.

Common sequence operations include indexing, sharding, adding, multiplication, and membership.

There are also built-in functions such as length (len), minimum value (min), and maximum value (mix ).

  • List

Basic list operations

# Create a list> nums = [1, 2, 3, 5]> nums [1, 2, 3, 5] # element assignment >>> nums [2] = 7 >>> nums [1, 2, 7, 5] >>># Delete element >>> del nums [0] >>> nums [2, 7, 5] # list function (type) you can create a list based on the string >>> st = list ('hello') >>> st ['h', 'E', 'l', 'l ', 'o'] # using indexes to access a single element, Using fragments to access a certain range of elements >>> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> nums [] [3, 4, 5, 6] ## to enable a shard to include the last element of the list, you must provide an index greater than the last element index as the boundary> nums [] [3, 4, 5, 6, 7, 8, 9, 10] ## count from the end> nums [-3:] [8, 9, 10] # count from the Start> nums [: 3] [1, 2, 3] # entire list> nums [:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # step size> nums [] [1, 4, 7, 10]> nums [: 4] [1, 5, 9] >>> nums [:-2] [10, 8, 6, 4, 2] ## partition assignment >>> name = list ('hemin') >>> name ['h', 'E', 'M', 'I ', 'N'] >>>> name [2:] = list ('shen') >>> name ['h', 'E', 's', 'h ', 'E', 'n'] # insert and delete> nums2 = []> nums2 [] = [2, 3, 4] >>> nums2 [1, 2, 3, 4, 5] >>> nums2 [] = [] >>> nums2 [1, 5]

List Method

# Append () append: directly modifies the original list, instead of returning the Modified list >>> letters = ['A', 'B ', "c"] >>> letters. append ('D') >>> letters ['A', 'B', 'C', 'D'] # count () count the number of occurrences of a specified element, letters = list ('abcdeeefggggh') >>> letters. count ('G') 4 # append multiple values to the end of the extend () list, alternatively, you can use the new list to expand the original list >>> letters2 = list ('ijklmn ') >>> letters. extend (letters2) >>> letters ['A', 'A', 'B', 'C', 'D', 'E', 'E ', 'E', 'F', 'G', 'h', 'I', 'J', 'k ', 'l', 'M', 'n'] # Note: Extend and append directly modify the original list, and the connection operation returns a new list, which is less efficient than the extend method >>> a = [1, 2, 3] >>> B = [, 6] >>> a + B [1, 2, 3, 4, 5, 6] >>> a [1, 2, 3] # a not changed # insert () insert> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> nums. insert (2, 'two') >>> nums [1, 2, 'two', 3, 4, 5, 6, 7, 8, 9, 10] # The pop method removes an element from the List (removed based on the index) >>> nums. pop () # remove the last element 10 by default> nums [1, 2, 'two', 3, 4, 5, 6, 7, 8, 9] >>> nums. pop (3) 3 >>> nums [1, 2, 'two', 4, 5, 6, The 7, 8, 9] # remove () remove method removes the first matching item of an element in the list (based on the element) >>> letters = list ('abccd ') >>> letters. remove ('C') >>> letters ['A', 'B', 'C', 'D'] # reverse () list element reverse storage> le = list ('abc')> le. reverse () >>> le ['C', 'B', 'a'] >>># list copy >>> x = [5, 4, 7] >>> y = x # in this way, x and y point to the same list >>> z = x [:] # copy it like this >>> x. sort () >>> y [4, 5, 7] >>> z [5, 4, 7] # sort () list sorting> num = [,]> num. sort ()> num [1, 2, 5, 6, 7, 9] # sor T (*, key = None, reverse = None), key indicates the sort keyword, and reserve indicates whether to reverse the order. The sort of Python3 is in stable sorting. >>> A = ['abc', 'abcd', 'AB']>. sort (key = len) >>> a ['AB', 'abc', 'abcd'] # clear () clear list # copy () copy list
  • Tuples
# Tuples cannot be modified # create tuples >>> 1, 2, 3 (1, 2, 3) >>> 'A', 'B', 'C' ('A ', 'B', 'C') >>>> () # null tuples () >>>> ('D',) # contain only one element ('D ',) # significance of tuples: tuples can be used as keys in ing, but the list does not work. tuples exist as methods and return values of many built-in functions.

 

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.