Python---basic data structure

Source: Internet
Author: User

List lists

Initialize [Vaule ...] List () an empty list

Data Conversion list (value ...)

Data read subscript operation [offset]

Data manipulation modify List[offset]=value slice List[start:end:step] Insert List.insert (offset, value) Delete del List[offset]/List.remove (val Ue

List.pop (offset=0) deletes the last one and returns this class. List.index (Vlaue) returns the offset of this value

Determines whether to include the value value in the list to return True or false to count value occurrences List.count (value)

List merge As String ', '. Join (list) returned with ', ' split string

The string str is split into List list = Str.split (', ') ', ' Split str. List.sort (reverse=false) sort.

Gets the list length len (list). Copy list newlist = List.copy () | NewList = list (list) | NewList = list[:].

NewList = The list does not have a copy and the operation is the same.

The list of properties can contain other lists, which can contain any type, because everything in Python is an object.

Tuple tuples

Initialize tuple TP = () |  TP = ' value ', | TP = Value1,value2,value3 | TP = Tuple (list)

Tuples can initialize multiple variables a,b,c = TP

Implementing element Exchange A, B = b,a

The value of the tuple cannot be changed after initialization. Tuples occupy less space than lists. The function parameter list is passed by tuple.

Dictionary Dictionary

Initialize dt = {} | DT = {Key:value,...}

Data conversion Two dollar list direct conversion dt = Dict ([[[A,b],[c,d],[e,f]]) Two tuple direct conversion dt = Dict ([(A, B), (C,d), (E,F)]) Two-dollar string dt = Dict ([' AB ', ' CD ', ' EF ') ])

Data manipulation Dt[key]=value dt.update (DT2) dictionary DT2 content copy to dt. DT2 values with the same key value are overwritten. Delete del Dt[key] empty dictionary dt.clear ()

Add new value Dt[nkey]=nvalue. Dt.keys () returns all keys Dt.values () returns all the values Dt.items returns a list of two tuples for all elements.

Dt.copy () Copy dictionary.

Determine if key exists key in DT returns TRUE or False

Set Set

Initialize _set = set () |   _set = {Val1,val2,...} The collection is unordered.

Data Conversion set (' str ') generates a set by character, repeating word conformance and set ([value]) using a list assignment set (value) to assign values to tuples

Operation Val in set to determine if Val exists set intersection A&b | A.intersection (b) set to set a | B | A.union (b)

Set seeking complement sets A.difference (b) Set to find the difference set the complement of the intersection of the set of-a A^b | A.symmetric_difference (b)

Whether the collection contains a <= B | A.issubset (b) | A >= B | A.issuperset (b) Any collection is its own superset.

A >b | A < b Judging whether it is a true superset

Python---basic data structure

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.