Details of common Python data structures

  • 2020-04-02 13:52:42
  • OfStack

This article lists and summarizes the common data structures of Python in detail, and illustrates them with examples, which is believed to be of certain reference value to readers.

On the whole The common data structures in Python can be collectively referred to as containers. Sequences (such as lists and tuples), maps (such as dictionaries), and sets (sets) are the three main containers.

I. sequences (lists, tuples, and strings)

Each element in a sequence has its own number. There are six types of built-in sequences in Python. Lists and tuples are the most common types. Others include strings, Unicode strings, buffer objects, and xrange objects. The following sections focus on the following tables, tuples, and strings.

1, lists,

Lists are mutable, which is the most important feature that distinguishes them from strings and tuples. In a nutshell, lists can be modified, but strings and tuples cannot.

(1) creation

Create a list by:


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

Output:


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

As you can see, this creation is very similar to arrays in javascript.

(2) list function

Using the list function (which is a type, not a function) is very effective for creating a list of strings:


list3=list("hello")
print list3

Output:


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

2, yuan group

A tuple, like a list, is a sequence, with the only difference being that tuples cannot be modified (which is also true of strings).

(1) creation


t1=1,2,3
t2="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 get:

A, comma separated some values, tuples are automatically created;

B. Tuples are enclosed in parentheses most of the time;

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

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

(2), tuple function

The tuple function is almost identical to the list function of a sequence: it takes a sequence (note the sequence) as an argument and converts it to a tuple. If the parameter is treated as a tuple, the parameter will return as is:


t1=tuple([1,2,3])
t2=tuple("jeff")
t3=tuple((1,2,3))
print t1
print t2
print t3
t4=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 < The module >
      T4 = the tuple (123).
TypeError: 'int' object is not iterable

3. String

(1) create


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

Output:


Hello world
H
H
e
l
l
o
 
w
o
r
l
d

(2) format

String formatting is implemented using the string formatting operator percent sign %.


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

The right operand of the formatter 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') # tuples 
str1='%s,%s' % strs
print str1
d={'h':'Hello','w':'World'} # The dictionary 
str1='%(h)s,%(w)s' % d
print str1

Output:


Hello,world
Hello,World

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


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

Output:


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

If we needed to output the special character %, we would no doubt think of escape, but the correct way to handle this in Python is as follows:


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

Output:


100%

To format Numbers, you usually need to control the width and precision of the output:


from math import pi
str1='%.2f' % pi # precision 2
print str1
str1='%10f' % pi # The field width 10
print str1
str1='%10.2f' % pi # The field width 10 Precision, 2
print str1

Output:


3.14
 3.141593
   3.14

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

Python also provides another way to format values in the string module: a template string. It works like the variable substitution in many UNIX shells, as shown below:


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

Output:


Hello,world!

If the replacement field is part of the word, the parameter name must be enclosed in parentheses to indicate the exact end:


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

Output:


Hello,world!

If you want to output $character, you can use goeast to output:


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

Output:


100$

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


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

Output:


Hello,world!

In addition to formatting, Python strings have many utilities built into them, which you can refer to in the official documentation, which is not listed here.

4. General sequence operation (method)

From a list, a tuple, and a string to "abstract" the sequence of some public common methods (not you imagine the CRUD), these operations include: index (indexing), shard (sliceing), add (adding), multiply (multiplying), and check whether an element belongs to a member of the sequence. In addition, there are also built-in functions to calculate the sequence length, 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]

The output


H
2
345

The index starts at 0 (left to right), and all sequences can be indexed this way. Amazingly, the index can start at the last position (from right to left) and is numbered -1:


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

Output:


o
3
123

(2) sharding

Sharding is used to access elements in a range. Sharding is achieved by two indexes separated by colons:


nums=range(10)
print nums
print nums[1:5]
print nums[6:10]
print nums[1:]
print nums[-3:-1]
print nums[-3:] # Includes the element at the end of the sequence, leaving the last index empty 
print nums[:] # Copy the whole 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 steps have different outputs:


nums=range(10)
print nums
print nums[0:10] # The default step size is 1  Is equivalent to nums[1:5 : 1]
print nums[0:10:2] # Step for 2
print nums[0:10:3] # Step for 3
 
##print nums[0:10:0] # Step for 0
print nums[0:10:-2] # Step for -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+str2
num1=[1,2,3]
num2=[2,3,4]
print num1+num2
print str1+num1

Output:


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

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

(4) multiplication


print [None]*10
str1='Hello'
print str1*2
num1=[1,2]
print num1*2
print 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 < The module >
      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 of a sequence (or of some other type) :


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

Output:


False
True
True

(6) length, maximum and minimum

The built-in functions len, Max, and min 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:


5
o
H
5
123
1

Ii. Mapping (dictionary)

Each element in the map has a name, which, as you know, is called a 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. The key must be unique. In Python, Numbers, strings, and tuples are all designed to be immutable, and common lists and sets are mutable, so lists and sets cannot be used as keys for dictionaries. Keys can be of any immutable type, which is where dictionaries in Python are most powerful.


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

Output:


{1: 1}

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

2. Automatic addition

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

3. Membership

The expression item in d (d for dictionary) looks for the key (containskey), not the value (containsvalue).

The Python dictionary is also powerful in that it has a lot of common methods built in, and you can refer to the official documentation, which is not listed here.

Consider: based on our experience with strongly typed languages such as C# and Java, we must ask whether dictionaries in Python are thread-safe.

Three, collections,

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


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

Collections appear to be built from sequences (or other iterable objects). Several important features and methods of the collection are as follows:

1. Copies are ignored

Collections are primarily used to check membership, so copies are ignored, as shown in the following example, and the contents of the output collection 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 collection elements is arbitrary

This is very much like a dictionary, and it's easy to understand that a set is a dictionary without value.


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([1,2,3])
set2=set([2,3,4])
set3=set1.union(set2)
print set1
print set2
print 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. Using the bitwise and (OR) operators "|" gives the same result:


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

The output is exactly the same as the union operation above.

Other common operations include & (intersection), < =, > =,-,copy(), etc., not listed here.


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

The output is as follows:


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

B, add and remove

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


set1=set([1])
print set1
set1.add(2)
print set1
set1.remove(2)
print set1
print set1
print 29 in set1
set1.remove(29) # Remove items that do not exist 

Output:


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

Traceback (most recent call last):
  File "F:\Python\test.py", line 9, in < The module >
      Set1.remove (29) # removes an item that does not exist
KeyError: 29

4, frozenset

Collections are mutable, so they cannot be used as keys for dictionaries. The collection itself can only contain 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 call last):
  File "F:\Python\test.py", line 3, in < The module >
      Characters. The add (set2)
TypeError: unhashable type: 'set'

You can use the type frozenset to represent an immutable (hashed) collection:


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

Output:


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

Related articles: