Common Python data structures

Source: Internet
Author: User
This article mainly introduces the common data structures of Python. For more information about the data structures of Python, see the following section, I believe it provides some reference value for 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
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 
 
    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
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
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
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
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
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])])

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.