A detailed description of Python common data structures

Source: Internet
Author: User
In this paper, we summarize the common data structure of Python, and attach examples to illustrate, I believe that readers have some reference value.

In general , the data structures common in Python can be collectively referred to as Containers (container). sequences (such as lists and tuples), mappings (such as dictionaries) , and collections (set) are the three main types of containers.

One, sequence (list, tuple, and string)

Each element in the sequence has its own number. There are 6 types of built-in sequences in Python. where lists and tuples are the most common types. Other include strings, Unicode strings, buffer objects, and Xrange objects. The following tables, tuples, and strings are highlighted below.

1. List

The list is mutable, which is the most important feature that distinguishes it from strings and tuples, in a nutshell: Lists can be modified, and strings and tuples cannot.

(1), create

You can create a list in the following ways:

list1=[' Hello ', ' World ']print list1list2=[1,2,3]print list2

Output:

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

As you can see, this is created in a very similar way to arrays in JavaScript.

(2), List function

It is very useful to create a list of strings through the list function (in fact, list is a type rather than a function):

List3=list ("Hello") print List3

Output:

[' H ', ' e ', ' l ', ' l ', ' O ']

2, meta-group

Tuples, like lists, are also a sequence, the only difference is that tuples cannot be modified (strings actually 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 we can analyze:

A, the comma separates some values, the tuple automatically creates completes;

B, tuples are mostly enclosed in parentheses;

C, the empty tuple can be represented by parentheses that do not contain content;

D, a tuple containing only one value, must be added with a comma (,);

(2), tuple function

The tuple function and the list function of a sequence are almost the same: take a sequence (note the sequence) as an argument and convert it to a tuple. If the parameter is a tuple, the parameter is returned as-is:

T1=tuple ([t2=tuple]) ("Jeff") T3=tuple ((+)) Print T1print T2print t3t4=tuple (123) Print T45

Output:

(1, 2, 3) (' J ', ' e ', ' f ', ' F ') (1, 2, 3)

Traceback (most recent):
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) formatting

String formatting is implemented using the string formatting operator, which is percent%.

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

The right operand of the format operator can be anything, and if it is a tuple or a mapping type (such as a dictionary), the string formatting will be different.

strs= (' Hello ', ' world ') #元组str1 = '%s,%s '% strsprint str1d={' h ': ' Hello ', ' W ': ' World ' #字典str1 = '% (h) s,% (w) s '% Dprint str1

Output:

Hello,worldhello,world

Note: if a tuple that needs to be converted exists as part of a transformation expression, it must be enclosed in parentheses :

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

Output:

Traceback (most recent): File "F:\Python\test.py", line 2, in 
 
  
   
    str1= '%s,%s '% ' Hello ', ' World ' typeer Ror:not enough arguments for format string
 
  

If you need to output% this special character, there is no doubt that we would think of escaping, but the correct way to handle Python is as follows:

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

Output:

100%

to format a number, you usually need to control the width and precision of the output :

From math import pistr1= '%.2f '% pi #精度2print str1str1= '%10f '% pi #字段宽10print str1str1= '%10.2f '% pi #字段宽10, accuracy 2print str1

Output:

3.14 3.141593   3.14

String formatting also includes many other rich conversion types, which can be found in the official documentation.

In Python, the string module also provides another way to format values: template strings. It works like a variable substitution in many Unix shells, as follows:

From string import templatestr1=template (' $x, $y! ') Str1=str1.substitute (x= ' Hello ', y= ' world ') print str1

Output:

hello,world!

If the replacement field is 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 $ character, you can use the $$ output:

From string import templatestr1=template (' $x $$ ') str1=str1.substitute (x= ') print str1

Output:

100$

In addition to keyword parameters, template strings can be formatted using dictionary variables that 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, the Python string also contains a number of practical methods that can be referenced in the official documentation, which is not listed here.

4. General sequence operation (method)

Some common common methods that can "abstract" out sequences from lists, tuples, and strings (not the crud you imagine) are: indexes (indexing), shards (sliceing), plus (adding), Multiply (multiplying) and check whether an element belongs to a member of a sequence. In addition, there are built-in functions such as calculating the length of the sequence and the maximum minimum element.

(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 at 0 (left-to-right), and all the sequences are indexed in this way. Amazingly, the index can start from the last position (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) sharding

A shard operation is used to access an element within a certain range. Shards are implemented by two indexes separated by a colon:

Nums=range (Ten) print Numsprint nums[1:5]print nums[6:10]print nums[1:]print nums[-3:-1]print nums[-3:] #包括序列结尾的元素, Empty last index print nums[:] #复制整个序列

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 steps, with different outputs:

Nums=range () print numsprint nums[0:10] #默认步长为1 equivalent to Nums[1:5:1]print Nums[0:10:2] #步长为2print Nums[0:10:3] #步长为3 # #print NUMS[0:10:0] #步长为0print nums[0:10:-2] #步长为-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):
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):
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 is used to check whether an object is a member (that is, an element) of a sequence (or other type):

str1= ' Hello ' print ' h ' in str1 print ' h ' in Str1num1=[1,2]print 1 in NUM1

Output:

Falsetruetrue

(6) Length, maximum minimum value

The built-in function Len, Max, and Min can return the number, maximum, and minimum elements of the 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

Second, mapping (dictionary)

Each element in the map has a name, as you know, the name of the professional named key. A dictionary (also known as a hash table) is the only built-in mapping type in Python.

1. Key type

The key of a dictionary can be a number, a string, or a tuple, and the key must be unique. In Python, numbers, strings, and tuples are designed to be immutable types, and common lists and collections (sets) are mutable, so lists and collections cannot be keys to the dictionary. The key can be any immutable 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):
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, it can be assigned a value so that the dictionary creates a new item.

3. Membership

The expression item in D (D is a dictionary) looks for the key (ContainsKey) instead of the value (Containsvalue).

The Python dictionary is also powerful in that it includes a number of common operating methods that can be found in the official documentation, which is not listed here.

Thinking: Based on our experience with strong-typed languages, such as C # and Java, we would definitely ask if the dictionaries in Python are thread-safe?

Three, the collection

Collections (set) are introduced in Python 2.3, and are typically created directly using newer versions of Python, as shown below:

Strs=set ([' Jeff ', ' Wong ', ' cnblogs ']) Nums=set (range (10))

It seems that a collection is built by a sequence (or other object that can be iterated). Several important features and methods of the collection are as follows:

1, the copy is ignored

Collections are primarily used to check membership, so replicas are ignored, as shown in the following example, and the contents of the output are the same.

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 collection elements is arbitrary

This is very similar to a dictionary, which can be easily understood as a dictionary with no value set.

Strs=set ([' Jeff ', ' Wong ', ' cnblogs ']) print STRs

The output is as follows:

Set ([' Wong ', ' cnblogs ', ' Jeff '])

3. Common methods of collection

A, intersection union

Set1=set ([Set2=set]) ([2,3,4]) set3=set1.union (set2) print Set1print set2print set3

Output:

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

The Union operation returns a set of two sets without altering the original collection. Use the bitwise AND (OR) operator "|" Can get the same result:

Set1=set ([Set2=set]) ([2,3,4]) set3=set1|set2print set1print set2print Set3

The output is the same as the above union operation.

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

Set1=set ([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

Very similar to the method of adding and removing sequences, refer to the official documentation:

Set1=set ([1]) print Set1set1.add (2) Print set1set1.remove (2) Print Set1print set1print in Set1set1.remove #移除不存在的项

Output:

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

Traceback (most recent):
File "F:\Python\test.py", line 9, in
Set1.remove () #移除不存在的项
Keyerror:29

4, Frozenset

The collection is mutable, so it cannot be used as a dictionary key. The collection itself can contain only immutable values, so it cannot contain other collections:

Set1=set ([1]) Set2=set ([2]) Set1.add (Set2)

The output is as follows:

Traceback (most recent):
File "F:\Python\test.py", line 3, in
Set1.add (Set2)
Typeerror:unhashable type: ' Set '

You can use the Frozenset type to represent a collection that is immutable (hashed):

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.