Python data structure

Source: Internet
Author: User

List (lists)


Create List

Mode 1: Directly Create thelist = [1,2,3,4,5,6,7,8,9] ==> [1,2,3,4,5,6,7,8,9]

Mode 2: Using the built-in method list (), the list method accepts a sequence parameter (for example, a tuple or a string)

1 thelist = List ((1,2,3,4,5,6,7,8,9)) ==> [1,2,3,4,5,6,7,8,9]

2 thelist = List ("ABCDEFG") ==> ["A", "B", "C", "D", "E", "F", "G"]


Add an element to the list

1 using the Append method of list

Thelist = []; Thelist.append (1) ==> [1]


2 Ways to use assignments

Thelist = []; Thelist[0:2] = [==>]


Finds the specified element in the list

1 using the Index method, returns the ordinal of the element in the list, such as the element is not in the list, and throws a ValueError exception

Thelist = [1,2,3,4,5,6,7,8]; Indexnum = Thelist.index (1) ==> indexNum-0


2 use in if the element exists in the list returns true if there is no return False

Thelist = [1,2,4]

1 in thelist ==> True

3 in Thelist ==> False



Removes the specified element from the list

1) Delete the element using the Del method

Thelist = [1,2,3,4,5,6]; Del (thelist[1]) ==> [1,3,4,5,6]


Calculate the length of a list

1) using the Len () method

Thelist = [1,2,3,4,5]; Len (thelist) ==> 5


Map (dictionary)

Map Each element contains two parts: Key (Key) and value. Each key in the dictionary is unique


Create a map

Themap = {"A": 1, "B": 2, "C": 3} ==>{' C ': 3, ' a ': 1, ' B ': 2}


Adding elements to a map

to Map adds a keyword "a". The keyword ' A ' corresponds to the value 1

Themap = {}

Themap["a"] = 1


Find the specified element in the map

Checks if the specified keyword exists in the map

Themap ={"A": 1, "B": 2, "C": 3}

Find out if keyword ' a ' is in map

' A ' in Themap ==> True

Find if the keyword ' k ' is in map

' K ' in Themap ==> False



Delete elements that are developed in the map

Themap = {"A": 1, "B": 2, "C": 3}

1 Delete the specified keyword ' b ' from the map

Del (themap[' B ']) ==> {' C ': 3, ' a ': 1}

2 Delete the keyword ' m ' from the map

Del (themap[' m ']) ==> keyerror: ' m ' throws an exception and cannot delete a nonexistent keyword


Calculate the length of a map

Themap = {"A": 1, "B": 2, "C": 3}

Len (themap) ==> 3



String (String)

Strings are immutable values


Assigning values to Strings

THESTR = "Sample Line"


formatting/linking of strings

Lineone = "One"

Linetwo = "both"

1 connect with + sign

Linethree = Lineone + linetwo ==> onetwo

2 Join () method join

'-'. Join ([lineone,linetwo]) ==> Onetwo

'-'. Join (lineone) ==> o-n-e

3 Using the Format method

1 linethree = "%s%s"% (lineone,linetwo) ==> Onetwo


2 "{0}{1}". Format (lineone,linetwo) ==> Onetwo


3 "{one}{two}". Format (one=lineone, both = linetwo) ==> onetwo


4 p = [Lineone,linetwo]

"{0[0]}{0[1]}". Format (p) ==>onetwo


5 string Segmentation (Splite)

Lineone.split () ==> [' O ', ' n ', ' E ']


6 string removal of whitespace characters (strip)

"ABC". Strip () ==> ' abc '


Extracting a string

Lineone[1:] = "NE"

Lineone[:-1] = "on"

LINEONE[1:-1] = "n"

K = "ABCDEFG"; K[0::2] ==> ' ACEG '


Conversion of strings to other data types


Float (str) float ("1e-1") results are 0.1
int (str) int ("12") result is 12
int (str,base) int ("11", 2) result is 3, 2 binary 11 is converted to decimal number

Int ("11", 8) result is 9, convert 8 binary 11 to decimal number


String Lookup

TMP = "ASDFGHJKL"

1 find

Tmp.find ("a") ==> 0 #字符存在于字符串中

Find.find ("z") ==>-1 #字符串不存在于字符串

2 Index

Tmp.index (' a ') ==> 0 #字符存在于字符串中

Tmp.index (' z ') ==> valueerror #字符串不存在于字符串



Python 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.