SoFunction
Updated on 2024-11-18

Summary of python string and common data structure knowledge

Using Strings

World War II led to the birth of the modern electronic computer, the idea was simple: use the computer to calculate the trajectory of a missile, so in the era when computers were just being born, the information that computers processed was mainly numerical, and the world's first computer, the ENIAC, was able to perform about 5,000 floating-point operations per second. Over time, although numerical operations are still one of the most important things that computers do on a daily basis, today's computers deal with more data in the form of textual information, and the way Python represents textual information, which we talked about a long time ago, is the string type. By string, we mean a finite sequence of zero or more characters, generally notated as $${\displaystyle s=a_{1}a_{2}\dots a_{n}(0\leq n \leq \infty)}$$.

We can understand the use of strings with the following code.

def main():
 str1 = 'hello, world!'
 # Calculate the length of a string with the len function
 print(len(str1)) # 13
 # Get a copy of the initial capitalization of the string
 print(()) # Hello, world!
 # Get a copy of the string in uppercase
 print(()) # HELLO, WORLD!
 # Find the location of a substring from a string
 print(('or')) # 8
 print(('shit')) # -1
 # Similar to find but throws an exception if no substring is found.
 # print(('or'))
 # print(('shit'))
 # Check if the string starts with the specified string
 print(('He')) # False
 print(('hel')) # True
 # Check if the string ends with the specified string
 print(('!')) # True
 # Center the string at the specified width and fill both sides with the specified characters
 print((50, '*'))
 # Place the string to the right with the specified width to fill the left side with the specified characters
 print((50, ' '))
 str2 = 'abc123456'
 # Remove the character at the specified position from the string (subscripting operation)
 print(str2[2]) # c
 # String slice (from specified start index to specified end index)
 print(str2[2:5]) # c12
 print(str2[2:]) # c123456
 print(str2[2::2]) # c246
 print(str2[::2]) # ac246
 print(str2[::-1]) # 654321cba
 print(str2[-3:-1]) # 45
 # Check if the string consists of numbers
 print(()) # False
 # Check if the string is alphabetic
 print(()) # False
 # Check if the string consists of numbers and letters
 print(()) # True
 str3 = ' jackfrued@ '
 print(str3)
 # Get a copy of the string trimmed of spaces on the left and right sides
 print(())


if __name__ == '__main__':
 main()

In addition to strings, Python has a variety of built-in types of data structures. If you want to save and manipulate data in a program, the vast majority of the time you can use existing data structures to do so, the most commonly used ones include lists, tuples, collections, and dictionaries.

usage list

The following code demonstrates how to define a list, access list elements using subscripts, and add and remove elements.

def main():
 list1 = [1, 3, 5, 7, 100]
 print(list1)
 list2 = ['hello'] * 5
 print(list2)
 # Calculate the length of the list (number of elements)
 print(len(list1))
 # Subscripting (indexing) operations
 print(list1[0])
 print(list1[4])
 # print(list1[5]) # IndexError: list index out of range
 print(list1[-1])
 print(list1[-3])
 list1[2] = 300
 print(list1)
 # Add elements
 (200)
 (1, 400)
 list1 += [1000, 2000]
 print(list1)
 print(len(list1))
 # Delete element
 (3)
 if 1234 in list1:
  (1234)
 del list1[0]
 print(list1)
 # Empty list elements
 ()
 print(list1)


if __name__ == '__main__':
 main()

As with strings, lists can also be sliced, through the slice operation we can realize the list of copies or a part of the list out to create a new list, the code is shown below.

def main():
 fruits = ['grape', 'apple', 'strawberry', 'waxberry']
	fruits += ['pitaya', 'pear', 'mango']
	# Loop over list elements
 for fruit in fruits:
  print((), end=' ')
 print()
 # List slicing
 fruits2 = fruits[1:4]
 print(fruits2)
 # fruit3 = fruits # no list copied only new reference created
 # The list can be copied with the full slice operation
 fruits3 = fruits[:]
 print(fruits3)
 fruits4 = fruits[-3:-1]
 print(fruits4)
 # A copy of the inverted list can be obtained by a reverse slice operation
 fruits5 = fruits[::-1]
 print(fruits5)


if __name__ == '__main__':
 main()

The following code implements a sorting operation on a list.

def main():
 list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
 list2 = sorted(list1)
 # The sorted function returns a sorted copy of the list without modifying the incoming list.
 # Functions should be designed to have as few side effects as possible, just like sorted functions.
 list3 = sorted(list1, reverse=True)
 # Specify sorting by string length instead of the default alphabetical order via the key keyword argument
 list4 = sorted(list1, key=len)
 print(list1)
 print(list2)
 print(list3)
 print(list4)
 # Send a sort message to the list object to sort directly on the list object
 (reverse=True)
 print(list1)


if __name__ == '__main__':
 main()

We can also create lists using the generative syntax for lists, as shown in the code below.

import sys


def main():
 f = [x for x in range(1, 10)]
 print(f)
 f = [x + y for x in 'ABCDE' for y in '1234567']
 print(f)
 # Create list containers with the list generator expression syntax
 # After creating a list with this syntax the elements are already ready so it takes more memory space
 f = [x ** 2 for x in range(1, 1000)]
 print((f)) # See how many bytes of memory an object occupies
 print(f)
 # Note that the following code creates a generator object instead of a list
 # Data can be fetched through the generator but it doesn't take up extra space to store the data
 # Get data through internal arithmetic every time you need it (takes extra time)
 f = (x ** 2 for x in range(1, 1000))
 print((f)) # Doesn't take up space to store data compared to generative generators
 print(f)
 for val in f:
  print(val)


if __name__ == '__main__':
 main()

In addition to the generator syntax mentioned above, there is another way to define a generator in Python, which is to transform a normal function into a generator function via the yield keyword. The following code demonstrates how to implement a generator that generates a Fibolache series. The so-called Fibolache series can be defined by the following recursive method:

$${\displaystyle F_{0}=0}$$

$${\displaystyle F_{1}=1}$$

$${\displaystyle F_{n}=F_{n-1}+F_{n-2}}({n}\geq{2})$$

def fib(n):
 a, b = 0, 1
 for _ in range(n):
  a, b = b, a + b
  yield a


def main():
 for val in fib(20):
  print(val)


if __name__ == '__main__':
 main()

Using Tuples

Python's tuples are similar to lists, except that the elements of a tuple can't be modified, and we've used tuples more than once in previous code. As the name implies, we form a tuple by combining multiple elements together, so it can hold multiple pieces of data just like a list. The following code demonstrates how to define and use tuples.

def main():
 # Define tuples
 t = ('Luo Hao', 38, True, 'Chengdu, Sichuan')
 print(t)
 # Get the elements of the tuple
 print(t[0])
 print(t[3])
 # Iterate over the values in the tuple
 for member in t:
  print(member)
 # Reassign tuples to values
 # t[0] = 'King Sledgehammer' # TypeError
 # variable t re-references the new tuple the original tuple will be garbage collected
 t = ('Wang Sledgehammer', 20, True, 'Kunming, Yunnan')
 print(t)
 # Convert tuples to lists
 person = list(t)
 print(person)
 # A list is able to modify its elements
 person[0] = 'Bruce Lee'
 person[1] = 25
 print(person)
 # Convert lists to tuples
 fruits_list = ['apple', 'banana', 'orange']
 fruits_tuple = tuple(fruits_list)
 print(fruits_tuple)


if __name__ == '__main__':
 main()

Here is a very interesting question to explore, why do we need a type like a tuple when we already have a data structure like a list?

1, the elements of the tuple can not be modified, in fact, we are in the project, especially in a multi-threaded environment (which will be discussed later) may prefer to use those unchanged objects (on the one hand, because the object state can not be modified, so you can avoid the unnecessary program errors caused by this, simply put, an unchanged object is easier to maintain than a mutable object; on the other hand, because there is not a On the other hand, because no thread can modify the internal state of an invariant object, an invariant object is automatically thread-safe, which eliminates the overhead of dealing with synchronization. (An invariant object can be easily shared for access). So the conclusion is: if you don't need to add, delete, or modify elements, you can consider using tuples, but of course if a method returns multiple values, using tuples is a good choice.

2, tuples are better than lists in terms of creation time and space occupied. We can use the getsizeof function of the sys module to check how much memory space is occupied by a tuple and a list of the same elements, which is easy to do. We can also use the magic command %timeit in ipython to analyze how much time it takes to create a tuple and a list with the same elements, the figure below shows the result of my test on macOS.

Using Collections

Sets in Python are consistent with mathematical sets, do not allow duplicate elements, and can perform operations such as intersection, union, and difference.

def main():
 set1 = {1, 2, 3, 3, 3, 2}
 print(set1)
 print('Length =', len(set1))
 set2 = set(range(1, 10))
 print(set2)
 (4)
 (5)
 ([11, 12])
 print(set1)
 print(set2)
 (5)
 # KeyError is raised if the removed element does not exist.
 if 4 in set2:
  (4)
 print(set2)
 # Traverse the collection container
 for elem in set2:
  print(elem ** 2, end=' ')
 print()
 # Convert tuples to collections
 set3 = set((1, 2, 3, 3, 2, 1))
 print(())
 print(set3)
 # Intersection, union, difference, and symmetric difference operations on sets
 print(set1 & set2)
 # print((set2))
 print(set1 | set2)
 # print((set2))
 print(set1 - set2)
 # print((set2))
 print(set1 ^ set2)
 # print(set1.symmetric_difference(set2))
 # Judging subsets and supersets
 print(set2 <= set1)
 # print((set1))
 print(set3 <= set1)
 # print((set1))
 print(set1 >= set2)
 # print((set2))
 print(set1 >= set3)
 # print((set3))


if __name__ == '__main__':
 main()

Description: Python allows some special methods to customize operators for certain types or data structures (will be discussed in later chapters), the above code we have a collection of arithmetic when you can call the collection object's methods, you can also directly use the corresponding operators, such as & operator with the intersection of the method is the same role, but the The use of operators makes the code more intuitive.

Using Dictionaries

Dictionary is another variable container model, similar to the dictionary we use in life, it can store any type of object, unlike lists and collections, each element of the dictionary is a "key-value pair" consisting of a key and a value, which are separated by a colon. The following code demonstrates how to define and use a dictionary.

def main():
 scores = {'Luo Hao': 95, 'White Yuanfang': 78, 'Dee': 82}
 # You can get the corresponding value in the dictionary by the key
 print(scores['Luo Hao'])
 print(scores['Dee'])
 # Iterate over the dictionary (it's really the keys that are iterated over and then the corresponding values are retrieved from the keys)
 for elem in scores:
  print('%s\t--->\t%d' % (elem, scores[elem]))
 # Update elements in the dictionary
 scores['White Yuanfang'] = 65
 scores["Zhuge Wanglang] = 71
 (naengmyeon (Korean dish based on cold noodles in soup)=67, Fang Qizhe (1936-), Daoist priest in Tang dynasty=85)
 print(scores)
 if 'Wu Zetian' in scores:
  print(scores['Wu Zetian'])
 print(('Wu Zetian'))
 # The get method also gets the corresponding value by key but can be set to a default value
 print(('Wu Zetian', 60))
 # Delete elements from the dictionary
 print(())
 print(())
 print(('Luo Hao', 100))
 # Empty the dictionary
 ()
 print(scores)


if __name__ == '__main__':
 main()

practice

Exercise 1: Displaying running text on the screen

import os
import time


def main():
 content = 'Beijing welcomes you to open up the sky for you ............'
 while True:
  # Clear the output on the screen
  ('cls') # ('clear')
  print(content)
  # Hibernate for 200 milliseconds
  (0.2)
  content = content[1:] + content[0]


if __name__ == '__main__':
 main()

Exercise 2: Design a function to generate a CAPTCHA of the specified length, which consists of upper and lower case letters and numbers.

import random


def generate_code(code_len=4):
 """
 Generate CAPTCHA of specified length

 :param code_len: Length of CAPTCHA(default (setting)4characters)

 :return: Random CAPTCHA consisting of upper and lower case letters and numbers
 """
 all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 last_pos = len(all_chars) - 1
 code = ''
 for _ in range(code_len):
  index = (0, last_pos)
  code += all_chars[index]
 return code

Exercise 3: Design a function to return the suffix of a given filename.

def get_suffix(filename, has_dot=False):
 """
 Get the filename suffix

 :param filename: filename
 :param has_dot: Whether or not the returned suffix needs a dot
 :return: the filename suffix of the file
 """
 pos = ('.')
 if 0 < pos < len(filename) - 1:
  index = pos if has_dot else pos + 1
  return filename[index:]
 else:
  return ''

Exercise 4: Design a function to return the values of the largest and second largest elements of an incoming list.

def max2(x):
 m1, m2 = (x[0], x[1]) if x[0] > x[1] else (x[1], x[0])
 for index in range(2, len(x)):
  if x[index] > m1:
   m2 = m1
   m1 = x[index]
  elif x[index] > m2:
   m2 = x[index]
 return m1, m2

Exercise 5: Calculate what day of the year the specified month and year are.

def is_leap_year(year):
 """
 Determine if the specified year is a leap year

 :param year: Year
 :return: True if the year is a leap year, False if it is a flat year.
 """
 return year % 4 == 0 and year % 100 != 0 or year % 400 == 0


def which_day(year, month, date):
 """
 Calculate what day of the year the incoming date is on

 :param year: Year
 :param month: month
 :param date: day
 :return: day
 """
 days_of_month = [
  [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
 ][is_leap_year(year)]
 total = 0
 for index in range(month - 1):
  total += days_of_month[index]
 return total + date


def main():
 print(which_day(1980, 11, 28))
 print(which_day(1981, 12, 31))
 print(which_day(2018, 1, 1))
 print(which_day(2016, 3, 1))


if __name__ == '__main__':
 main()

Exercise 6: Print the Yang Hui triangle.

def main():
 num = int(input('Number of rows: '))
 yh = [[]] * num
 for row in range(len(yh)):
  yh[row] = [None] * (row + 1)
  for col in range(len(yh[row])):
   if col == 0 or col == row:
    yh[row][col] = 1
   else:
    yh[row][col] = yh[row - 1][col] + yh[row - 1][col - 1]
   print(yh[row][col], end='\t')
  print()


if __name__ == '__main__':
 main()

Comprehensive Case

Case 1: Double-color ball selection

from random import randrange, randint, sample


def display(balls):
 """
 Outputting a list of bicolor numbers
 """
 for index, ball in enumerate(balls):
  if index == len(balls) - 1:
   print('|', end=' ')
  print('%02d' % ball, end=' ')
 print()


def random_select():
 """
 Randomly select a set of numbers
 """
 red_balls = [x for x in range(1, 34)]
 selected_balls = []
 selected_balls = sample(red_balls, 6)
 selected_balls.sort()
 selected_balls.append(randint(1, 16))
 return selected_balls


def main():
 n = int(input('A few bets on the machine:'))
 for _ in range(n):
  display(random_select())


if __name__ == '__main__':
 main()

Description: The above uses the sample function of the random module to select n non-repeating elements from a list.

Synthesis Case 2: The Joseph Ring Problem

"""
The Lucky Christians.
There were 15 Christians and 15 non-Christians who were shipwrecked at sea. In order to let some of them survive, they had to throw 15 of them into the sea. One man thought of a way to do it, that is, they all formed a circle, and a certain person started to count from 1, and the one who counted to 9 was thrown into the sea, and the one who was behind him then started to count from 1, and the one who counted to 9 was continued to be thrown into the sea, until 15 of them were thrown out. Thanks to God's blessing, all 15 Christians were spared, ask these people how they stood at the very beginning, which positions were Christians and which positions were non-Christians.
"""


def main():
 persons = [True] * 30
 counter, index, number = 0, 0, 0
 while counter < 15:
  if persons[index]:
   number += 1
   if number == 9:
    persons[index] = False
    counter += 1
    number = 0
  index += 1
  index %= 30
 for person in persons:
  print('Base' if person else 'Not', end='')


if __name__ == '__main__':
 main()

Synthesis Case 3: Tic-Tac-Toe

import os


def print_board(board):
 print(board['TL'] + '|' + board['TM'] + '|' + board['TR'])
 print('-+-+-')
 print(board['ML'] + '|' + board['MM'] + '|' + board['MR'])
 print('-+-+-')
 print(board['BL'] + '|' + board['BM'] + '|' + board['BR'])


def main():
 init_board = {
  'TL': ' ', 'TM': ' ', 'TR': ' ',
  'ML': ' ', 'MM': ' ', 'MR': ' ',
  'BL': ' ', 'BM': ' ', 'BR': ' '
 }
 begin = True
 while begin:
  curr_board = init_board.copy()
  begin = False
  turn = 'x'
  counter = 0
  ('clear')
  print_board(curr_board)
  while counter < 9:
   move = input('It's %s turn to move, please enter a position: ' % turn)
   if curr_board[move] == ' ':
    counter += 1
    curr_board[move] = turn
    if turn == 'x':
     turn = 'o'
    else:
     turn = 'x'
   ('clear')
   print_board(curr_board)
  choice = input('One more game.?(yes|no)')
  begin = choice == 'yes'


if __name__ == '__main__':
 main()