List of Python data structures and tuples

Source: Internet
Author: User

List of Python data structures and tuples
  1. Sequence: A sequence is a data structure that contains elements that are numbered (starting at 0). A typical sequence consists of a list, a string, and a tuple. Where the list is mutable (can be modified), and tuples and strings are immutable (once created are fixed). The sequence contains 6 built-in sequences, including lists, tuples, strings, Unicode strings, buffer objects, and Xrange objects.

    列表的声明:mylist = []

    2. Operation of the list:

     (1) Fragment of sequence: usage: mylist[startindex:endindex:step] exam:mylist[2:10] retrieves the 2nd character to the 10th character, the default step is 1.    MYLIST[2:10:2] retrieves the 2nd character through the 10th character, specifying a step of 2. Mylist[-2:-1:2] A positive index is the coordinate relative to the header, and the negative number is the coordinate relative to the tail.    In fact, the coordinates must be smaller than the terminating coordinates, otherwise the empty shards are returned. Mylist[-12:-2:-2] Steps can also be negative, which means extracting elements from right to left. (2) Index of sequence: usage: mylist[index] exam:mylist[2] mylist[-2] PS: Positive number is relative to the header's coordinates, negative numbers are relative to the tail coordinates. (3) Sequence addition: usage: Mylist1 + mylist2 <==> [+] + [3,4] (4) Sequence multiplication: Usage: mylist * 5 mylist element repeats 5 times. (5) In Operator: Usage: ' Item ' in MyList to determine if MyList contains a member. 

3. The built-in functions that are involved in the list: the built-in function len, Min, Max is useful for list operations.

(1) len函数返回序列中所包含元素的数量。(2) min函数和max函数分别返回学列中最大和最小元素。(3) list函数可以把字符串转换成列表。    exam: list(‘hello‘) => [‘H‘,‘e‘,‘l‘,‘l‘,‘o‘](4) cmp函数用来比较2个元素的大小     exam: cmp(x,y) => 返回0表示相等, -1 则是 x < y  1 则是 x > y(5) reversed函数对序列进行反向迭代。(6) sorted 返回已排序的包含seq所有元素的列表。

4. List method:

(1) The Append:append method appends a new object to the end of the list.    exam:lst=[1,2,3] Lst.append (4) and [1,2,3,4] (2) The Count:count method counts the number of occurrences of an element in the list. exam:x=[[1,2],1,1,[2,1,[1,2]] X.count (1) = 1 (3) The Extend:extend method can append multiple values of another sequence at the end of the list.    That is, you can extend the existing list with a new list.    exam:a=[1,2,3] b=[4,5,6] A.extend (b) [1,2,3,4,5,6] (4) The Index:index method is used to find the index position of the first occurrence of a value from the list. exam:lst=[' we ', ' Le ', ' at '] lst.index (' le ') + 1 (5) Insert:insert method is used to insert an object into the list: exam:lst=[1,2,3,4,5,6] Lst.insert ( 3,8) = [1,2,3,8,4,5,6] (6) Pop:pop method removes an element from the list (the last one by default) and returns the element. (7) The Remove:remove method removes the first occurrence of a value in the list: exam:x=[' to ', ' being ', ' or '] x.remove (' to ') = you know. (8) The reverse method deserializes the elements in the list.     (9) The Sort method is used to sort the list at the original location.        The Exam:sort method has a default sorting method, plus the use of advanced sorting, the sort method has two optional parameters, key and Reverse,key specify the sorted keyword parameter, and the ordering is sorted by the size of the key, reverse is used to specify whether to reverse the order.        X.sort (Key=len) = = is sorted by the length of the string.        X.sort (reverse=true) = = reverse order. X.sort (CMP) = Specifies the sort function, you know.

5. Tuples: Tuples and lists are also a series. The only difference is that tuples cannot be modified.

(1) 声明方式:    用逗号分隔一些值,就自动创建了元组。 exam: 1,2,3 => (1,2,3)    也可以通过园括号声明。 exam: (1,2,3) => (1,2,3)(2) 元组的乘法:    3*(40+2) => (42,42,42)

6. The built-in functions involved in the tuple:

(1)tuple函数的功能和list函数的基本上是一样的:以一个序列作为参数并把它转换为元组。    exam: tuple([1,2,3]) => tuple(1,2,3)

7. Sharding of tuples:

exam: x=1,2,3  x[1] => 2  x[0:2] => (1,2)

List of Python data structures and tuples

Related Article

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.