Strings and common data structures

Use string

World War II led to the birth of the modern computer, the first computer was used to calculate the trajectory of the missile, and many years after the birth of the computer, the computer processing of information are basically numeric information. The first computer in the world called ENIAC (Electronic Numerical Integrator and Computer), was born at the University of Pennsylvania, about 5,000 times per second to complete floating-point operations. Over time, though the numerical calculation is still one of the computers in the daily work of the most important things, but today's computer processing of data may have been even more text exists in the way if we want to operate the program by Python these text messages, you must first understand the type string and the knowledge associated with it.

The so-called string , is a finite sequence of zero or more characters, generally referred to as $${\displaystyle s=a_{1}a_{2}\dots a_{n}(0\leq n \leq \infty)}$$. In Python program, if we put a single or plurality of characters surrounded by single or double quotes, a character string can be represented.

s1 = 'hello world'
s2 = "hello world"
# 以三个双引号或单引号开头的字符串可以折行
s3 = """
hello, 
world
"""
print(s1, s2, s3, end='')

It can be used in the string \(backslash) to represent an escape, that is \behind the character is no longer its original meaning, for example: \nnot representative and the backslash character n, but for new line; and \tis not representative of and the backslash character t, but the tab character. So if you want to represent in the string 'to be written \', with the ideal representation \to be written \\. You can run the following code and see what the output.

s1 = '\'hello,world\''
s2 = '\n\\hello,world\\\n'
print(s1, s2, end='')

In \the back can be followed by an octal or hexadecimal digits to represent characters, for example \141, and \x61represents lowercase letters a, the former is the octal notation, which is the hexadecimal notation. It can also be \followed by the Unicode character encoding to represent characters, for example, \u722c\u866bon behalf of the Chinese "reptile." Run the following code and see what the output.

s1 = '\141\142\143\x61\x62\x63'
s2 = '\u722c\u866b'
print(s1, s2)

If you do not want to string \representation escaped, we can put the letters in front of the string rto be explained, and then look at what the following code will output.

s1 = r'\'hello, world!\''
s2 = r'\n\\hello, world!\\\n'
print(s1, s2, end='')

Python provides a very rich operator of type string, we can use the +operator to achieve string concatenation, may be used *operator to duplicate the contents of a character string, may be used in, and not into determine whether a character string comprising a further string (operation member), we can also use []and [:]operators removed some character or characters (slicing operation) from the string, as shown in the code below.

s1 = 'hello ' * 3
print(s1)  # hello hello hello
s2 = 'world'
s1 += s2
print(s1)  # hello hello hello world
print('ll' in s1)  # True
print('good' in s1)  # False
str2 = 'abc123456'
# 从字符串中取出指定位置的字符(下标运算)
print(str2[2])  # c
# 字符串切片(从指定的开始索引到指定的结束索引)
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

In Python, we can also be accomplished through a series of string processing method, the code is shown below.

str1 = 'hello, world!'
# 通过内置函数len计算字符串的长度
print(len(str1)) # 13
# 获得字符串首字母大写的拷贝
print(str1.capitalize()) # Hello, world!
# 获得字符串每个单词首字母大写的拷贝
print(str1.title()) # Hello, World!
# 获得字符串变大写后的拷贝
print(str1.upper()) # HELLO, WORLD!
# 从字符串中查找子串所在位置
print(str1.find('or')) # 8
print(str1.find('shit')) # -1
# 与find类似但找不到子串时会引发异常
# print(str1.index('or'))
# print(str1.index('shit'))
# 检查字符串是否以指定的字符串开头
print(str1.startswith('He')) # False
print(str1.startswith('hel')) # True
# 检查字符串是否以指定的字符串结尾
print(str1.endswith('!')) # True
# 将字符串以指定的宽度居中并在两侧填充指定的字符
print(str1.center(50, '*'))
# 将字符串以指定的宽度靠右放置左侧填充指定的字符
print(str1.rjust(50, ' '))
str2 = 'abc123456'
# 检查字符串是否由数字构成
print(str2.isdigit())  # False
# 检查字符串是否以字母构成
print(str2.isalpha())  # False
# 检查字符串是否以数字和字母构成
print(str2.isalnum())  # True
str3 = '  [email protected] '
print(str3)
# 获得字符串修剪左右两侧空格之后的拷贝
print(str3.strip())

Before mentioned, the following manner may be formatted output string.

a, b = 5, 10
print('%d * %d = %d' % (a, b, a * b))  # 5 * 10 = 50

Of course, we can also provide a method to complete the string format string, the code shown below.

a, b = 5, 10
print('{0} * {1} = {2}'.format(a, b, a * b))  # 5 * 10 = 50

After Python 3.6, as well as the format string more concise writing style, that is, before the string with the letter f, we can use the following syntax sugar to simplify the above code.

a, b = 5, 10
print(f'{a} * {b} = {a * b}')    # 5 * 10 = 50

In addition to the string, Python also built a plurality of types of data structures, and if the operation to save the data in the program, most of the time can use existing data structure to achieve, including a list of the most commonly used, tuple, set, and dictionary.

Use the list

I do not know if you noticed, we just talked about the string type ( str) and numeric types before we talked about ( intand float) there are some differences. An integer is a scalar type, the internal structure of this type of object that is not accessible; the string type is a structured, non-scalar type, it will have a set of attributes and methods. Next, we will introduce the list ( list), is a structured, non-scalar type, which is an ordered sequence of values, each value can be identified, defined list can be a list of elements is placed through the index [], the a plurality of elements ,separated may be used forloop traversal list elements may be used []or [:]operator to remove one or more list elements.

The following code demonstrates how to define a list of how to traverse the index calculation lists and lists.

list1 = [1, 3, 5, 7, 100]
print(list1)  # [1, 3, 5, 7, 100]
# 乘号表示列表元素的重复
list2 = ['hello'] * 3
print(list2)  # ['hello', 'hello', 'hello']
# 计算列表长度(元素个数)
print(len(list1))  # 5
# 下标(索引)运算
print(list1[0])  # 1
print(list1[4])  # 100
# print(list1[5])  # IndexError: list index out of range
print(list1[-1])  # 100
print(list1[-3])  # 5
list1[2] = 300
print(list1)  # [1, 3, 300, 7, 100]
# 通过循环用下标遍历列表元素
for index in range(len(list1)):
    print(list1[index])
# 通过for循环遍历列表元素
for elem in list1:
    print(elem)
# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
for index, elem in enumerate(list1):
    print(index, elem)

The following code demonstrates how to add elements to the list and how to remove elements from the list.

list1 = [1, 3, 5, 7, 100]
# 添加元素
list1.append(200)
list1.insert(1, 400)
# 合并两个列表
# list1.extend([1000, 2000])
list1 += [1000, 2000]
print(list1) # [1, 400, 3, 5, 7, 100, 200, 1000, 2000]
print(len(list1)) # 9
# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
if 3 in list1:
    list1.remove(3)
if 1234 in list1:
    list1.remove(1234)
print(list1) # [1, 400, 5, 7, 100, 200, 1000, 2000]
# 从指定的位置删除元素
list1.pop(0)
list1.pop(len(list1) - 1)
print(list1) # [400, 5, 7, 100, 200, 1000]
# 清空列表元素
list1.clear()
print(list1) # []

And like strings, lists slicing operation may be done, we can achieve copy list by slicing operation or taken out of the part of the list to create a new list, the code shown below.

fruits = ['grape', 'apple', 'strawberry', 'waxberry']
fruits += ['pitaya', 'pear', 'mango']
# 列表切片
fruits2 = fruits[1:4]
print(fruits2) # apple strawberry waxberry
# 可以通过完整切片操作来复制列表
fruits3 = fruits[:]
print(fruits3) # ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya', 'pear', 'mango']
fruits4 = fruits[-3:-1]
print(fruits4) # ['pitaya', 'pear']
# 可以通过反向切片操作来获得倒转后的列表的拷贝
fruits5 = fruits[::-1]
print(fruits5) # ['mango', 'pear', 'pitaya', 'waxberry', 'strawberry', 'apple', 'grape'] 

The following code implements a sort operation on the list.

list1 = ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
list2 = sorted(list1)
# sorted函数返回列表排序后的拷贝不会修改传入的列表
# 函数的设计就应该像sorted函数一样尽可能不产生副作用
list3 = sorted(list1, reverse=True)
# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
list4 = sorted(list1, key=len)
print(list1)  # ['orange', 'apple', 'zoo', 'internationalization', 'blueberry']
print(list2)  # ['apple', 'blueberry', 'internationalization', 'orange', 'zoo']
print(list3)  # ['zoo', 'orange', 'internationalization', 'blueberry', 'apple']
print(list4)  # ['zoo', 'apple', 'orange', 'blueberry', 'internationalization']
# 给列表对象发出排序消息直接在列表对象上进行排序
list1.sort(reverse=True)
print(list1)  # ['zoo', 'orange', 'internationalization', 'blueberry', 'apple']

Formula and generators

We can also use the list to generate syntax to create the list, the code shown below.

f = [x for x in range(1, 10)]
print(f)
f = [x + y for x in 'ABCDE' for y in '1234567']
print(f)
# 用列表的生成表达式语法创建列表容器
# 用这种语法创建列表之后元素已经准备就绪所以需要耗费较多的内存空间
f = [x ** 2 for x in range(1, 1000)]
print(sys.getsizeof(f))  # 查看对象占用内存的字节数
print(f)
# 请注意下面的代码创建的不是一个列表而是一个生成器对象
# 通过生成器可以获取到数据但它不占用额外的空间存储数据
# 每次需要数据的时候就通过内部的运算得到数据(需要花费额外的时间)
f = (x ** 2 for x in range(1, 1000))
print(sys.getsizeof(f))  # 相比生成式生成器不占用存储数据的空间
print(f)
for val in f:
    print(val)

In addition to generating syntax, Python there is another embodiment defined generator mentioned above, is through yieldkeyword transformed into a common function generator function. The following code demonstrates how to implement a generation Feibolaqie series generator. The number of columns may be called by the following Feibolaqie recursively be defined methods:

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

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

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

img

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()

Tuple

Python tuple list is similar to the data type to a container, it can be a variable (object) to store a plurality of data, except that the elements of the tuple can not be modified in the previous code we have used more than once a tuple. As the name suggests, we have multiple elements combined together to form a tuple, so it can be saved as a list of pieces of data. The following code demonstrates how to define and use a tuple.

# 定义元组
t = ('骆昊', 38, True, '四川成都')
print(t)
# 获取元组中的元素
print(t[0])
print(t[3])
# 遍历元组中的值
for member in t:
    print(member)
# 重新给元组赋值
# t[0] = '王大锤'  # TypeError
# 变量t重新引用了新的元组原来的元组将被垃圾回收
t = ('王大锤', 20, True, '云南昆明')
print(t)
# 将元组转换成列表
person = list(t)
print(person)
# 列表是可以修改它的元素的
person[0] = '李小龙'
person[1] = 25
print(person)
# 将列表转换成元组
fruits_list = ['apple', 'banana', 'orange']
fruits_tuple = tuple(fruits_list)
print(fruits_tuple)

Here's a question worth exploring, we have a list of such data structure, why do we need such a tuple type it?

  1. Tuple elements can not be modified, in fact, we are in the project, especially a multi-threaded environment (will be mentioned later) may prefer to use are those immutable objects (objects on the one hand because the state can not be modified, can be avoided unnecessary procedural errors resulting, simply means that a constant than a variable target object easier to maintain; on the other hand because no one thread can modify the internal state of the same object, a constant target automatic thread-safe, so you can save processing synchronization overhead. a constant object can be easily shared access). So the conclusion is this: If the elements do not need to add, delete, modify, they can consider using tuples, of course, if a method to return multiple values, use a tuple is a good choice.
  2. Tuple space created above takes time and is superior to the list. We can use the sys module getsizeof function to check the tuple same elements of memory and how much memory a list of each space, this is very easy to do. We can also use magic instruction in ipython% timeit to analyze the same time create content tuples and lists it takes, the lower figure is the result of the test on my macOS system.

img

Use collection

Python is a collection with a set of mathematical is the same, it does not allow duplicate elements but may be an intersection, union, difference and other operations.

img

Collections can be created and used in the manner shown in the following code.

# 创建集合的字面量语法
set1 = {1, 2, 3, 3, 3, 2}
print(set1)
print('Length =', len(set1))
# 创建集合的构造器语法(面向对象部分会进行详细讲解)
set2 = set(range(1, 10))
set3 = set((1, 2, 3, 3, 2, 1))
print(set2, set3)
# 创建集合的推导式语法(推导式也可以用于推导集合)
set4 = {num for num in range(1, 100) if num % 3 == 0 or num % 5 == 0}
print(set4)

And removing elements from the collection add an element to the collection.

set1.add(4)
set1.add(5)
set2.update([11, 12])
set2.discard(5)
if 4 in set2:
    set2.remove(4)
print(set1, set2)
print(set3.pop())
print(set3)

Member of the collection, intersection, union, difference and other operations.

# 集合的交集、并集、差集、对称差运算
print(set1 & set2)
# print(set1.intersection(set2))
print(set1 | set2)
# print(set1.union(set2))
print(set1 - set2)
# print(set1.difference(set2))
print(set1 ^ set2)
# print(set1.symmetric_difference(set2))
# 判断子集和超集
print(set2 <= set1)
# print(set2.issubset(set1))
print(set3 <= set1)
# print(set3.issubset(set1))
print(set1 >= set2)
# print(set1.issuperset(set2))
print(set1 >= set3)
# print(set1.issuperset(set3))

Description: the Python allowed in some special way to custom operators (will be mentioned later chapters) of a certain type or data structure, when the above code, we set of methods for computing a set of objects can be called, can directly use the corresponding operators such as &the role of the operator with the method of intersection is the same, but use the operator make the code more intuitive.

Use a dictionary

Dictionary is another variable container model, Python dictionary with the dictionary to use in our lives is the same as the same, it can store any type of object, and a list of different sets, each element of the dictionary are from a key, and a value of "key on" key and value separated by a colon. The following code demonstrates how to define and use a dictionary.

# 创建字典的字面量语法
scores = {'骆昊': 95, '白元芳': 78, '狄仁杰': 82}
print(scores)
# 创建字典的构造器语法
items1 = dict(one=1, two=2, three=3, four=4)
# 通过zip函数将两个序列压成字典
items2 = dict(zip(['a', 'b', 'c'], '123'))
# 创建字典的推导式语法
items3 = {num: num ** 2 for num in range(1, 10)}
print(items1, items2, items3)
# 通过键可以获取字典中对应的值
print(scores['骆昊'])
print(scores['狄仁杰'])
# 对字典中所有键值对进行遍历
for key in scores:
    print(f'{key}: {scores[key]}')
# 更新字典中的元素
scores['白元芳'] = 65
scores['诸葛王朗'] = 71
scores.update(冷面=67, 方启鹤=85)
print(scores)
if '武则天' in scores:
    print(scores['武则天'])
print(scores.get('武则天'))
# get方法也是通过键获取对应的值但是可以设置默认值
print(scores.get('武则天', 60))
# 删除字典中的元素
print(scores.popitem())
print(scores.popitem())
print(scores.pop('骆昊', 100))
# 清空字典
scores.clear()
print(scores)

Exercise

Exercise 1: Marquee display text on the screen.

Answer:

import os
import time


def main():
    content = '北京欢迎你为你开天辟地…………'
    while True:
        # 清理屏幕上的输出
        os.system('cls')  # os.system('clear')
        print(content)
        # 休眠200毫秒
        time.sleep(0.2)
        content = content[1:] + content[0]


if __name__ == '__main__':
    main()

Exercise 2: Design of a generating function specified length codes, capitalization codes consists of letters and numbers.

Answer:

import random


def generate_code(code_len=4):
    """
    生成指定长度的验证码

    :param code_len: 验证码的长度(默认4个字符)

    :return: 由大小写英文字母和数字构成的随机验证码
    """
    all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    last_pos = len(all_chars) - 1
    code = ''
    for _ in range(code_len):
        index = random.randint(0, last_pos)
        code += all_chars[index]
    return code

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

Answer:

def get_suffix(filename, has_dot=False):
    """
    获取文件名的后缀名

    :param filename: 文件名
    :param has_dot: 返回的后缀名是否需要带点
    :return: 文件的后缀名
    """
    pos = filename.rfind('.')
    if 0 < pos < len(filename) - 1:
        index = pos if has_dot else pos + 1
        return filename[index:]
    else:
        return ''

Exercise 4: Design a function return values ​​passed in the list of the largest and second largest element.

Answer:

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 the date specified in the first few days of the year.

Answer:

def is_leap_year(year):
    """
    判断指定的年份是不是闰年

    :param year: 年份
    :return: 闰年返回True平年返回False
    """
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0


def which_day(year, month, date):
    """
    计算传入的日期是这一年的第几天

    :param year: 年
    :param month: 月
    :param date: 日
    :return: 第几天
    """
    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 Pascal's Triangle .

Answer:

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()

Integrated Case

Case 1: Pick color ball.

from random import randrange, randint, sample


def display(balls):
    """
    输出列表中的双色球号码
    """
    for index, ball in enumerate(balls):
        if index == len(balls) - 1:
            print('|', end=' ')
        print('%02d' % ball, end=' ')
    print()


def random_select():
    """
    随机选择一组号码
    """
    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('机选几注: '))
    for _ in range(n):
        display(random_select())


if __name__ == '__main__':
    main()

Description: Use random sample module to achieve the above functions will not be repeated selecting n elements from the list.

Integrated Case 2: Josephus problem .

"""
《幸运的基督徒》
有15个基督徒和15个非基督徒在海上遇险,为了能让一部分人活下来不得不将其中15个人扔到海里面去,有个人想了个办法就是大家围成一个圈,由某个人开始从1报数,报到9的人就扔到海里面,他后面的人接着从1开始报数,报到9的人继续扔到海里面,直到扔掉15个人。由于上帝的保佑,15个基督徒都幸免于难,问这些人最开始是怎么站的,哪些位置是基督徒哪些位置是非基督徒。
"""


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('基' if person else '非', end='')


if __name__ == '__main__':
    main()

Integrated Case 3: Tic-tac-toe game.

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
        os.system('clear')
        print_board(curr_board)
        while counter < 9:
            move = input('轮到%s走棋, 请输入位置: ' % turn)
            if curr_board[move] == ' ':
                counter += 1
                curr_board[move] = turn
                if turn == 'x':
                    turn = 'o'
                else:
                    turn = 'x'
            os.system('clear')
            print_board(curr_board)
        choice = input('再玩一局?(yes|no)')
        begin = choice == 'yes'


if __name__ == '__main__':
    main()

Guess you like

Origin www.cnblogs.com/EricZLin/p/12500840.html