Python Learning-a list of basic data structures

Source: Internet
Author: User

List of basic data structures

List lists are an ordered set of elements that can be added and removed at any time. There are similarities to arrays in C and Java, but arrays in C and Java must hold elements of the same type, whereas in Python's list, the elements stored can be of different types. Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.

1. Representation of the list

Create a list by enclosing separate data items separated by commas in square brackets.

list1 = [‘str‘,‘中文‘,123,‘Python‘]list2 = [2,3,5,78,999]list3 = [[‘str1‘,‘str2‘],‘abcde‘,‘pppp‘]    #一个列表作为另一个列表中的一个元素
2. Features of the list

1. List Index
The symbol is: [Indexed value]

list = [‘str11‘,‘str22‘,‘str33‘,‘str44‘]

(1). Forward index

>>> list[0]‘str11‘>>> list[1]‘str22‘>>> list[3]‘str44‘

(2). Reverse Index

>>> list[-1]‘str44‘>>> list[-2]‘str33‘

2. Slicing the list
As with a slice of a string, each character in a string is used as the base unit, and each element in the list is separated by commas as the basic unit.

list[start:end:step]    # 从start位置开始到end-1位置结束, 步长为step;                        - 如果start省略, 则从头开始切片;                        - 如果end省略, 一直切片到字符串最后;
>>> list = [111,222,333,44,55,66,77,88]>>> list[::]     #切片全部[111, 222, 333, 44, 55, 66, 77, 88]>>> list[:-1]    #从第一个元素到倒数第2个元素[111, 222, 333, 44, 55, 66, 77]>>> list[:-2]    #从第一个元素到倒数第3个元素[111, 222, 333, 44, 55, 66]>>> list[::2]    #步长为2[111, 333, 55, 77]>>> list[::-1]   #步长为负数,从末尾开始切片[88, 77, 66, 55, 44, 333, 222, 111]>>> list[::-2]   #步长为-2[88, 66, 44, 222]

3. Duplication of lists
The symbol is: *

>>> list = [‘aa‘,‘bb‘,‘cc‘]>>> list*3[‘aa‘, ‘bb‘, ‘cc‘, ‘aa‘, ‘bb‘, ‘cc‘, ‘aa‘, ‘bb‘, ‘cc‘]

4. List of connections
The symbol is: +

>>> a = [‘ab‘,‘ef‘,‘gg‘]>>> b = [‘nice‘,‘good‘,‘great‘,‘perfect‘]>>> a+b[‘ab‘, ‘ef‘, ‘gg‘, ‘nice‘, ‘good‘, ‘great‘, ‘perfect‘]

5. Member Operators
The symbols are: in VS not in

>>> list = [‘abc‘,‘egg‘,‘yyy‘,‘完美‘]>>> ‘abc‘ in listTrue>>> ‘a‘ in listFalse>>> ‘我‘ not in listTrue>>> ‘完美‘ not in listFalse
3. Update the list

The symbol is: the value of the assignment symbol =

>>> list = [‘人生苦短‘,‘我选python‘,‘2223333‘,‘+-*/‘]      >>> list[3] = 123456>>> list[‘人生苦短‘, ‘我选python‘, ‘2223333‘, 123456]>>> list[0] = ‘pypy‘>>> list[‘pypy‘, ‘我选python‘, ‘2223333‘, 123456]
4. Nested lists

Nested lists contain other lists in the list

1. Creating Nested columns
(1). Create directly:

>>> list1 = [[‘ab‘,‘cd‘,‘ee‘],[12,34,56],[‘p‘,‘y‘,‘t‘]]>>> list1[[‘ab‘, ‘cd‘, ‘ee‘], [12, 34, 56], [‘p‘, ‘y‘, ‘t‘]]

(2). Indirect creation:

>>> a = [1,2,34,66]>>> b = [‘ww‘,‘tt‘,‘yy‘]>>> c = [‘df‘,‘hh‘,99]>>> list2 = [a,b,c]>>> list2[[1, 2, 34, 66], [‘ww‘, ‘tt‘, ‘yy‘], [‘df‘, ‘hh‘, 99]]

2. Index of nested lists
Similar to a two-dimensional array in C and Java. Use a combination of brackets to index. The index of the nested list is interesting. It is a combined operation of slices and indexes. (In fact, I think the index essence is the slice, but the slice end value is start+1, and the step is 1)
You can only query one element of a sub-list in a nested list or multiple primary colors at a time, and you cannot query an element of the uniform index location of multiple sub-lists, and if you do this, the query result will be a sub-list.

5. List of built-in functions
Function name description
len (list) list element number
max (list) return list element maximum
min (list) returns the minimum value of the list element
list (seq) Converts a tuple to a list
list.append (obj) adds a new object at the end of the list
list.count (obj) counts the number of occurrences of an element in a list
list.extend (seq) Appends multiple values from another sequence at the end of the list (the original list is expanded with a new list)
list.index (obj) Find a value from the list index position of the first occurrence
list.insert (index, obj) Inserts an object into the list
List.pop ([index=-1]]) removes an element from the list (the last element by default) and returns the value of the element
list.remove (obj) Removes the first occurrence of a value in a list
list.reverse () Reverse list elements
List.sort (Cmp=none, Key=none, Reverse=false) sort the original list
list.clear () empty list
list.copy () copy list

Python Learning-a list of basic data structures

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.