Python Basic data structure

Source: Internet
Author: User

Python is a dynamic language

List: array-like, ordered set

Create: L = [' Michael ', +, True], you can include a variety of data in a list

Access: accessed by index, such as l[0]; can be accessed in reverse order, such as l[-1], to access the last element of the list (attention to cross-border issues)

Add new element: Append () method, append element to the end of list, e.g. L.append (' Lisa ')

Insert () method, add element to the specified position, e.g. L.insert (0, ' Lisa '), add ' Lisa ' to the position indexed to 0

Delete element: Pop () method, no parameter, delete the last element of list, such as L.pop (), will print true

When there are parameters, delete the corresponding element, such as L.pop (1), then delete the element with index 1

Replacement element: Direct assignment, e.g. l[0]= ' Lisa '

       

Tuple: There is a sequence list, Chinese translation into tuples, once created can not be modified

Created: T = (' Adam ', ' Lisa ', ' Bart ')

Gets the tuple element in the same way as List

The "variable" tuple:tuple contains variable elements,

such as T = (' A ', ' B ', [' A ', ' B '])

L = t[2]

L[0] = ' X '

L[1] = ' Y '

       At this point the tuple changes

On the surface, the elements of a tuple do change, but in fact it is not a tuple element, but a list element. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change. Point to ' a ', it cannot be changed to point to ' B ', point to a list, cannot be changed to point to other objects, but the list itself is variable

Dict: Contains key and value, similar to array, key value is not repeatable, key-value to no order, key element is not variable

Create:

D = {    ' Adam ': +,    ' Lisa ': $,    ' Bart ': 59}

Access: You can simply use the form of D[key] to find the corresponding value, such as d[' Adam ';

Determine if key exists: if ' Adam ' in D: ...

Use the Get () method: D.get (' Adam ') and return none if key does not exist

For loop traversal

Update: d[' Paul '] = 72, overwrite if key exists

Set: Holds a series of elements, which is similar to the list, but the set elements are not duplicated and are unordered, which is like the Dict key.

Create: Call set () and pass in a list,list element as the Set element: S=set ([1, 2, 3, 4])

Access: Set stores unordered collections, so we cannot access them by index.   Accessing an element in a set actually determines whether an element is in the set. ' Bart ' in S

The For loop implementation traverses for name in S: ... print name

Added: Add () method, such as S.add (4), if add content already exists, will not error, will not continue to add

Delete: Remove () method, such as S.remove (4), if the deletion does not exist, then error, so before the deletion to determine:

If name in S:        s.remove (name)

 

Python Basic data structure

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.