Python Common Data Structure explanation, python Data Structure

Source: Internet
Author: User

Python Common Data Structure explanation, python Data Structure

This article lists in detail the common Python data structures and provides examples to illustrate them. It is believed that it will be of reference value to readers.

OverallCommon Data Structures in Python can be collectively referred to as containers ).Sequences (such as lists and meta-groups), ing (such as dictionaries), and set are three main containers.

I. Sequence (list, tuples, and strings)

Each element in the sequence has its own number. Python has six built-in sequences. Lists and metadata are the most common types. Others include strings, Unicode strings, buffer objects, and xrange objects. The following describes the list, tuples, and strings.

1. List

The list is variable, which is the most important feature that distinguishes it from strings and metadata. In a word, the list can be modified, but the string and metadata cannot.

(1) Create

You can create a list using the following method:

list1=['hello','world']print list1list2=[1,2,3]print list2

Output:

['hello', 'world'][1, 2, 3]

As you can see, the creation method is very similar to the array in javascript.

(2) list Functions

The list function (in fact, list is a type rather than a function) is very effective for creating a list of strings:

list3=list("hello")print list3

Output:

['h', 'e', 'l', 'l', 'o']

2. tuples

Like lists, tuples are also a sequence. The only difference is that tuples cannot be modified (strings also have this feature ).

(1) Create

t1=1,2,3t2="jeffreyzhao","cnblogs"t3=(1,2,3,4)t4=()t5=(1,)print t1,t2,t3,t4,t5

Output:

(1, 2, 3) ('jeffreyzhao', 'cnblogs') (1, 2, 3, 4) () (1,)

From the above analysis, we can conclude that:

A. Values are separated by commas (,). The tuples are automatically created;

B. Most of the tuples are enclosed in parentheses;

C. Empty tuples can be represented by parentheses without content;

D. Single-value tuples must contain commas (,);

(2) tuple Function

The tuple function is almost the same as the list function of the sequence: Take a sequence (note that it is a sequence) as a parameter and convert it to a tuples. If the parameter is a tuple, the parameter is returned as is:

t1=tuple([1,2,3])t2=tuple("jeff")t3=tuple((1,2,3))print t1print t2print t3t4=tuple(123)print t45

Output:

(1, 2, 3)('j', 'e', 'f', 'f')(1, 2, 3)

Traceback (most recent call last ):
File "F: \ Python \ test. py", line 7, in <module>
T4 = tuple (123)
TypeError: 'int' object is not iterable

3. String

(1) Create

str1='Hello world'print str1print str1[0]for c in str1:  print c

Output:

Hello worldHHello world

(2) Format

String formatting is implemented using the string formatting operator % Percent.

str1='Hello,%s' % 'world.'print str1

The right operand of the formatting operator can be anything. If it is a tuples or ing type (such as a dictionary), the string formatting will be different.

Strs = ('hello', 'World') # tuples str1 = '% s, % s' % strsprint str1d = {'H': 'hello', 'w ': 'World'} # dictionary str1 = '% (h) s, % (w) s' % dprint str1

Output:

Hello,worldHello,World

Note: If the tuples to be converted exist as part of the conversion expression, they must be enclosed in parentheses:

str1='%s,%s' % 'Hello','world'print str1

Output:

Traceback (most recent call last): File "F:\Python\test.py", line 2, in <module>  str1='%s,%s' % 'Hello','world'TypeError: not enough arguments for format string

If the special character % needs to be output, we will undoubtedly think of escaping, but the correct processing method in Python is as follows:

str1='%s%%' % 100print str1

Output:

100%

To Format a number, you usually need to control the width and accuracy of the output:

From math import pistr1 = '%. 2f '% pi # precision 2 print str1str1 =' % 10f' % pi # field width 10 print str1str1 = '% 10.2f' % pi # field width 10, precision 2 print str1

Output:

3.14 3.141593   3.14

String formatting also contains many other conversion types. For details, refer to the official documentation.

In Python, the string module also provides another method for formatting values: Template string. It works like replacing variables in many UNIX shells, as shown below:

from string import Templatestr1=Template('$x,$y!')str1=str1.substitute(x='Hello',y='world')print str1

Output:

Hello,world!

If the field to be replaced is a part of a word, the parameter name must be enclosed in parentheses to accurately indicate the end:

from string import Templatestr1=Template('Hello,w${x}d!')str1=str1.substitute(x='orl')print str1

Output:

Hello,world!

To output the $ operator, you can use the $ output:

from string import Templatestr1=Template('$x$$')str1=str1.substitute(x='100')print str1

Output:

100$

In addition to keyword parameters, the template string can also be formatted using dictionary variables to provide key-value pairs:

from string import Templated={'h':'Hello','w':'world'}str1=Template('$h,$w!')str1=str1.substitute(d)print str1

Output:

Hello,world!

In addition to formatting, Python strings also have many built-in practical methods. For more information, see the official documentation.

4. General sequence operations (methods)

Some common methods (not CRUD) of sequences can be abstracted from lists, tuples, and strings. These operations include: Index and sliceing), add, multiply, and check whether an element belongs to a Sequence member. In addition, there are also built-in functions such as the length of the computing sequence and the maximum and minimum elements.

(1) Index

str1='Hello'nums=[1,2,3,4]t1=(123,234,345)print str1[0]print nums[1]print t1[2]

Output

H2345

The index starts from 0 (left to right). All sequences can be indexed in this way. What's amazing is that the index can start from the last position (from right to left) and the number is-1:

str1='Hello'nums=[1,2,3,4]t1=(123,234,345)print str1[-1]print nums[-2]print t1[-3]

Output:

o3123

(2) fragment

The partition operation is used to access elements within a certain range. Sharding is implemented using two indexes separated by colons:

Nums = range (10) print numsprint nums [] print nums [] print nums [1:] print nums [-3:-1] print nums [-3:] # contains the elements at the end of the sequence. Leave the last index blank. print nums [:] # copy the entire sequence.

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][1, 2, 3, 4][6, 7, 8, 9][1, 2, 3, 4, 5, 6, 7, 8, 9][7, 8][7, 8, 9]

Different step sizes have different outputs:

Nums = range (10) print numsprint nums [] # The default step size is 1, which is equivalent to nums [] print nums [] # The step size is 2 print nums [] # The step size is 3 ## numprint s [am] # The step size is 0 print nums [0: 10:-2] # The step size is-2

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][0, 1, 2, 3, 4, 5, 6, 7, 8, 9][0, 2, 4, 6, 8][0, 3, 6, 9][]

(3) sequence Addition

str1='Hello'str2=' world'print str1+str2num1=[1,2,3]num2=[2,3,4]print num1+num2print str1+num1

Output:

Hello world[1, 2, 3, 2, 3, 4]

Traceback (most recent call last ):
File "F: \ Python \ test. py", line 7, in <module>
Print str1 + num1
TypeError: cannot concatenate 'str' and 'LIST' objects

(4) Multiplication

print [None]*10str1='Hello'print str1*2num1=[1,2]print num1*2print str1*num1

Output:

[None, None, None, None, None, None, None, None, None, None]HelloHello[1, 2, 1, 2]

Traceback (most recent call last ):
File "F: \ Python \ test. py", line 5, in <module>
Print str1 * num1
TypeError: can't multiply sequence by non-int of type 'LIST'

(5) Membership

The in operator checks whether an object is a member (that is, an element) of a sequence (or another type ):

str1='Hello'print 'h' in str1 print 'H' in str1num1=[1,2]print 1 in num1

Output:

FalseTrueTrue

(6) length, maximum and minimum

The built-in functions len, max, and min are used to return the number, maximum, and minimum of elements contained in the sequence.

str1='Hello'print len(str1) print max(str1)print min(str1)num1=[1,2,1,4,123]print len(num1) print max(num1)print min(num1)

Output:

5oH51231

Ii. ing (dictionary)

Each element in the ing has a name. As you know, this name is a professional name called a key. A dictionary (also called a hash) is the only built-in ing type in Python.

1. Key type

Dictionary keys can be numbers, strings, or tuples. Keys must be unique. In Python, numbers, strings, and metadata are designed to be immutable types, while common lists and sets are variable. Therefore, lists and sets cannot be used as Dictionary keys. Keys can be any unchangeable type, which is the most powerful dictionary in Python.

list1=["hello,world"]set1=set([123])d={}d[1]=1print dd[list1]="Hello world."d[set1]=123print d

Output:

{1: 1}

Traceback (most recent call last ):
File "F: \ Python \ test. py", line 6, in <module>
D [list1] = "Hello world ."
TypeError: unhashable type: 'LIST'

2. automatically add

Even if the key does not exist in the dictionary, you can assign a value to it so that the dictionary creates a new item.

3. Membership

The expression item in d (d is a dictionary) looks for the containskey instead of the value (containsvalue ).

The Python dictionary has many built-in common operation methods. For more information, see the official documentation.

Thinking: based on our experience in using strong-type languages, such as C # and Java, we will certainly ask if the dictionary in Python is thread-safe?

Iii. Set

Set is introduced in Python 2.3 and can be directly created using a newer version of Python, as shown below:

strs=set(['jeff','wong','cnblogs'])nums=set(range(10))

It seems that a set is constructed by sequences (or other iteratable objects. Several important features and methods of the set are as follows:

1. The copy is ignored.

The set is mainly used to check the membership, so the copy is ignored. As shown in the following example, the output set contains the same content.

set1=set([0,1,2,3,0,1,2,3,4,5])print set1 set2=set([0,1,2,3,4,5])print set2

The output is as follows:

set([0, 1, 2, 3, 4, 5])set([0, 1, 2, 3, 4, 5])

2. The order of the Set elements is random.

This is very similar to the dictionary. You can simply understand a set as a dictionary without value.

strs=set(['jeff','wong','cnblogs'])print strs

The output is as follows:

set(['wong', 'cnblogs', 'jeff'])

3. Set common methods

A. Intersection union

set1=set([1,2,3])set2=set([2,3,4])set3=set1.union(set2)print set1print set2print set3

Output:

set([1, 2, 3])set([2, 3, 4])set([1, 2, 3, 4])

The union operation returns the union of two sets without changing the original set. Use the bitwise and (OR) operator "|" to get the same result:

set1=set([1,2,3])set2=set([2,3,4])set3=set1|set2print set1print set2print set3

Output the same result as the preceding union operation.

Other common operations include & (intersection), <=, >=,-, copy (), and so on.

set1=set([1,2,3])set2=set([2,3,4])set3=set1&set2print set1print set2print set3print set3.issubset(set1)set4=set1.copy()print set4print set4 is set1

The output is as follows:

set([1, 2, 3])set([2, 3, 4])set([2, 3])Trueset([1, 2, 3])False

B. add and remove

The method for adding and removing sequences is very similar. For details, refer to the official documentation:

Set1 = set ([1]) print set1set1. add (2) print set1set1. remove (2) print set1print set1print 29 in set1set1. remove (29) # remove a nonexistent item

Output:

set([1])set([1, 2])set([1])set([1])False

Traceback (most recent call last ):
File "F: \ Python \ test. py", line 9, in <module>
Set1.remove (29) # Remove a nonexistent item
KeyError: 29

4. frozenset

The set is variable, so it cannot be used as a dictionary key. The set itself can only contain unchangeable values, so it cannot contain other sets:

set1=set([1])set2=set([2])set1.add(set2)

The output is as follows:

Traceback (most recent call last ):
File "F: \ Python \ test. py", line 3, in <module>
Set1.add (set2)
TypeError: unhashable type: 'set'

The frozenset type can be used to represent an unchangeable (hashed) set:

set1=set([1])set2=set([2])set1.add(frozenset(set2))print set1

Output:

set([1, frozenset([2])])

How to build a Python Data Structure

Python Data Structure 1. list: variable value assignment method: shoplist = ['apple', 'mango ', 'carrot', 'Banana'] 2. array: variable value assignment method: zoo = ('Wolf ', 'elephant', 'penguin') 3. dictionary dict: Variable assignment mode: d = {key1: value1, key2: value2} 4. sequence: List, tuples, and strings are all sequences. (1) index OPERATOR: Get a character (2) of an element or string in a list/tuples. (2) segment OPERATOR: get a slice of a sequence, continuous element/character (3) eg: name = 'swaroop 'print 'characters0is ', name [0] # 's' index operator, similar to C # print 'characters1to3is ', name [] 'wa' slicing operator, similar to the Substring method in C #. In fact, Python contains a lot of content, such as Python and traditional The interpreted script language is different. It will be compiled into bytecode during the first execution, and then runs the Code directly. net dlr is a bit similar to the source code sky, and similar to Java virtual machines. In short, the code is converted into a way closer to the machine code, which can improve performance.

Python Data Structure

S [I: j: k] slice of s from I to j with step k
Reference: docs.python.org/..xrange

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.