Python101: 7. Data Structures and Sequences

post thumb
Python
by Admin/ on 05 Jul 2021

Python101: 7. Data Structures and Sequences

A data structure in Python is a collection of data elements that are organized in some way, and these data elements can be numbers, characters, or even other data structures.

In Python, the most basic data structures are sequences (lists and tuples), where each element in a sequence has a serial number (the specific location of the element), which is called an index, and the index subscript starts at 0, and so on …… This article focuses on sequences in Python and their practical applications.


Sequences Overview


  • There are six kinds of built-in sequences in Python. Of these, lists and tuples are the most common types. The others include strings, Unicode strings, buffer objects, and xrange objects.
  • The main difference between lists and tuples is that lists can be modified while tuples can’t. If you want to add elements on demand, then lists are better suited, but when sequences can’t be modified, using tuples is more appropriate, and using tuples is related to the way Python works.
  • Lists can replace tuples in almost all cases in Python, but not in special cases (when using a tuple as an unmodifiable key for a dictionary, the key cannot be modified, so you can’t use a list)

For example.

# Define a sequence of students
stuinfo=['zhangsan', 'lisi', 'wangwu', 18, 20]

Also the sequence can contain sequences, for example database: database

# Define student name and student age, then define a database of your own to add the two lists to
stuname=['zhangsan','lisi','wangwu']
stuage=[18,20,16]
database=[stuname,stuage]
database
# [['zhangsan', 'lisi', 'wangwu'], [18, 20, 16]]

Note: Python also has a data structure called a container, which can contain any other object; containers consist of two main categories: sequences and mappings (e.g., dictionaries). Each element of a sequence has its own number, while each element of a map has a name called a “key”. Collections are another type of container (more on this in a later section).

General Sequence Operations

All sequence types in Python can perform some specific operations, including indexing, slicing, adding, multiplying, and checking whether an element is a member of a sequence –> membership testing.

In addition, Python has built-in functions for calculating the length of a sequence and finding the largest and smallest elements.

1. Indexing

All elements in a sequence are numbered, and these numbers start from 0 and increase in order. Accessing these elements is done by subscripts, and this number is the index, e.g.

database[0]
# ['zhangsan', 'lisi', 'wangwu']
database[1]
# [18, 20, 16]


# index of string sequence
str='hello'
str[0]
# 'h'
str[1]
# 'e'

Note: A string is a sequence of characters with index 0 pointing to the first element This is the index above, all sequences can be indexed by way of element numbering, when using negative indexes, Python will do all from right to left, -1 is from the last element of the sequence, as follows.

# Start with the last element
str[-1]
# 'o'
# Start from the penultimate element
str[-2]
# 'l'

2. Slicing

Again similar to indexing, slicing is a colon operation to access elements within a certain range, e.g.

# Construct a sequence tag containing an element
tag=['https://www.cnblogs.com/yangyuqig/p/10101663.html']

# Get this element and take out a range of values by slicing
tag[0][0:24]
# 'https://www.cnblogs.com/'

As known above, the implementation of the slice operation needs to provide two indexes as boundaries, a left-closed-right-open interval, i.e. the 1st index is contained within the slice, while the 2nd index is not contained within this slice, e.g.

num=[1,2,3,4,5,6,7,8,9,10]
# means from the fourth to the last element
num[3:10]
# [4, 5, 6, 7, 8, 9, 10]

In addition to the above scheme, it is also possible to operate by displaying

2.1 Slice shortcut operations
num[0:3]
# Fetch the first 3 data
# [1, 2, 3]
2.2 Slicing step operations

A slice operation can set a step to an element, specifying the corresponding step to fetch the element at the beginning and end, e.g.

# return the element between the 1st and 6th by step 2
num[0:6:2]
# [1, 3, 5]

Another thing to note is that the negative step traverses the entire sequence from the end of the element to the front, so the start index of the negative slice must be greater than the end index

num[7:-1]
# [8, 9]

When the start index and end index are negative start so must be less than the end index.

num[-9:-1]
# [2, 3, 4, 5, 6, 7, 8, 9]

For a positive step, Python will extract the elements from the head of the sequence to the right, straight to the last element, while for a negative step, it will extract the elements from the tail of the sequence to the left, straight to the first, e.g.:

# Extract the first 6 elements in steps of 2
num[:6:2]
# [1, 3, 5]
# Extracts the first 8 elements from back to front in steps of 2
num[:2:-2]
# [10, 8, 6, 4]

3. Sequence summation

Sequence summation performs a concatenation operation between sequences and sequences by means of the plus sign “+”.

'hello'+' world !
# 'hello world !'

[1,2,3]+['zhangsan','lisi','wangwu']
# [1, 2, 3, 'zhangsan', 'lisi', 'wangwu']

Note: Only sequences of the same type can be concatenated

4. Sequence multiplication

A number x multiplied by a sequence will produce a new sequence, the original sequence will be reset to x times

['hello'+' world !'] *3
# ['hello world ! , 'hello world ! , 'hello world !']

5. Membership

Checking whether an element is in a sequence is done using the operator in. The in operator returns a boolean value that checks for a condition and returns true if it is true, otherwise it returns false, e.g.

str='hello'
'h' in str
# True

'x' in str
# False

6. Sequence length, maximum and minimum values

The sequence length, maximum and minimum values are checked using the built-in functions len, max and min. len returns the number of elements contained in the sequence, max and min return the maximum and minimum elements of the sequence, respectively

len([11,34,23])
# 3
max(11,34,23)
# 34
min(11,34,23)
# 11

Summary


This section gives you an introduction to the use of Python data structures of sequences, which provides support for Python engineers to use to take out the corresponding elements in practical applications in their projects.


Reference

https://github.com/JustDoPython/python-100-day/tree/master/day-007

comments powered by Disqus