10 月 222020
 
# 列表操作方式的效率比较
import time

a = []
# t0 = time.clock()  # 异常:AttributeError: module 'time' has no attribute 'clock'
# https://docs.python.org/3/whatsnew/3.8.html
# https://docs.python.org/3/library/time.html#time.perf_counter
# 该函数在python3.3废弃,在python3.8移除
t0 = time.perf_counter()
for i in range(1, 20000):
    a.append(i)
print(time.perf_counter() - t0, 'seconds process time')

t0 = time.perf_counter()
b = [i for i in range(1, 20000)]
print(time.perf_counter() - t0, 'seconds process time')
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/hello.py
0.0017573999999999992 seconds process time
0.0005343000000000014 seconds process time

Process finished with exit code 0
# 列表操作方式的效率比较
import time

a = []
# t0 = time.clock()  # 异常:AttributeError: module 'time' has no attribute 'clock'
# https://docs.python.org/3/whatsnew/3.8.html
# https://docs.python.org/3/library/time.html#time.perf_counter
# 该函数在python3.3废弃,在python3.8移除
t0 = time.perf_counter()  # 调用函数返回精确浮点性能计数值,两次调用返回值的差计算程序运行时间
for i in range(1, 20000):
    a.append(i)
t1 = time.perf_counter()
print('T0 is', t0)
print('T1 is', t1)
print(t1 - t0, 'seconds process time')

t0 = time.perf_counter()
b = [i for i in range(1, 20000)]
print(time.perf_counter() - t0, 'seconds process time')
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/hello.py
T0 is 0.0509396
T1 is 0.052513
0.0015733999999999956 seconds process time
0.0005889999999999992 seconds process time

Process finished with exit code 0
10 月 222020
 
# -*- coding:utf-8 -*-
# 数据结构,包括列表lists,字典dictionaries,元组tuples,集合sets

# 数据结构中的推导式或列表解析式
# 将10个元素装入列表,普通写法
a = []
for i in range(1, 11):
    a.append(i)
print(a)

# 列表解析式写法(方便,高效)
b = [i for i in range(1, 11)]
print(b)
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/datastructure.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Process finished with exit code 0
10 月 222020
 
# -*- coding:utf-8 -*-
# 数据结构,包括列表lists,字典dictionaries,元组tuples,集合sets

# 元组tuples中的元素是固定的,不可以修改,但可以查看索引
letters = ('a', 'b', 'c', 'd', 'e', 'f')
print(letters[0])

# 集合中的元素是无序的,不重复的任意对象
# 集合可以判断数据的从属关系
# 集合可以把数据结构中重复的元素减掉
# 集合不能被切片也不能被索引
a_set = {1, 2, 3, 4}
a_set.add(5)
print(a_set)
a_set.discard(4)
print(a_set)

# 对列表中的元素进行排序
num_list = [6, 2, 7, 4, 1, 3, 5]
print(sorted(num_list))  # 排序函数sorted()
print(sorted(num_list, reverse=True))  # 倒序排序

# 使用zip()函数同时操作多个列表
a = [3, 6, 9]
b = ['x', 'y', 'z']
# zip()函数的参数为迭代器(iterable)即可迭代的对象,可以为一个或多个
c = zip(a, b)  # zip()函数将对象中对应的元素打包成一个个元组tuples,然后返回由这些元组组成的对象
print(c)  # zip函数的返回值是zip类的对象,可以通过list()强制转为list列表
print(list(c))
# 多重循环
for a, b in zip(a, b):
    print(b, 'is', a)
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/datastructure.py
a
{1, 2, 3, 4, 5}
{1, 2, 3, 5}
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
<zip object at 0x0000029CD11E7080>
[(3, 'x'), (6, 'y'), (9, 'z')]
x is 3
y is 6
z is 9

Process finished with exit code 0
10 月 222020
 
# -*- coding:utf-8 -*-
# 数据结构,包括列表lists,字典dictionaries,元组tuples,集合sets

# 字典中的数据必须是以键值对形式出现
# 键(key)不可以重复,而值(value)可以重复
# 键是不可变的,也就是无法修改的
nasdaq_code = {
    'BIDU': 'Baidu',  # 【pycharm:PEP 8: E231 missing whitespace after ':' 键值对冒号右侧有空格】
    'SINA': 'Sina',
    'YOKU': 'Youku'
}
print(nasdaq_code)

# 字典中的键值不会有重复,相同键值只出现一次
a = {'key': 123, 'key': 123}  # 【pycharm:Dictionary contains duplicate keys 'key'】
print(a)

# 与列表不同,字典没有添加单一元素的方法
nasdaq_code['FB'] = 'Facebook'
print(nasdaq_code)

# 字典中添加多个元素的方法
nasdaq_code.update({'AMZN': 'Amazon', 'TSLA': 'Tesla'})
print(nasdaq_code)

# 字典中删除元素使用del方法
del nasdaq_code['AMZN']
print(nasdaq_code)

# 字典使用花括号,但索引内容时使用方括号
b = nasdaq_code['TSLA']
print(b)
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/datastructure.py
{'BIDU': 'Baidu', 'SINA': 'Sina', 'YOKU': 'Youku'}
{'key': 123}
{'BIDU': 'Baidu', 'SINA': 'Sina', 'YOKU': 'Youku', 'FB': 'Facebook'}
{'BIDU': 'Baidu', 'SINA': 'Sina', 'YOKU': 'Youku', 'FB': 'Facebook', 'AMZN': 'Amazon', 'TSLA': 'Tesla'}
{'BIDU': 'Baidu', 'SINA': 'Sina', 'YOKU': 'Youku', 'FB': 'Facebook', 'TSLA': 'Tesla'}
Tesla

Process finished with exit code 0

10 月 222020
 
# -*- coding:utf-8 -*-
# 数据结构,包括列表lists,字典dictionaries,元组tuples,集合sets
# https://docs.python.org/3/tutorial/datastructures.html

Weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(Weekday[0])

# 列表中的每一个元素都是可变的
# 列表中的元素是有序的,也就是每一个元素都有一个位置
# 列表可以容纳Python中的任何对象

all_in_list = [
    1,  # 整数(integer numbers)
    1.0,  # 浮点数(floating point numbers)
    'a word',  # 字符串(strings)
    print(168),  # 函数(functions)
    True,  # 布尔值(boolean value)
    [1, 2],  # 列表中套列表(lists)
    (1, 2),  # 元组(tuples)
    {'key': 'value'}  # 字典(dictionaries)
]

# 列表插入,插入元素(item)的位置(position)是指定元素位置之前的位置
fruit = ['pineapple', 'pear']
fruit.insert(1, 'grape')  # 插入方法method
print(fruit)

# 列表插入的另一种方法
fruit[0:0] = ['Orange']  # 列表最前面,列表索引
print(fruit)

# 列表删除
cars = ['benz', 'bmw', 'audi', 'byd']
cars.remove('bmw')  # 删除方法method
print(cars)

# 替换修改
cars[0] = 'ford'  # 替换第1个元素benz,列表索引
print(cars)

# 列表删除的另一种方法是,使用del关键字
del cars[0:2]  # 删除第1-2个元素ford和audi,列表索引
print(cars)

# 字符串分片和列表索引,都支持正反两种索引方式
# 正向索引编号Positive Index Number和反向索引编号Negative Index Number
# https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3

# 元素周期表(列表索引)
periodic_table = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne']
print(periodic_table[0])  # H
print(periodic_table[-2])  # F
print(periodic_table[0:3])  # H He Li
print(periodic_table[-10:-7])  # H He Li
print(periodic_table[-10:])  # 打印全部
print(periodic_table[:9])  # H - F 打印第1-8个元素
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/datastructure.py
Monday
168
['pineapple', 'grape', 'pear']
['Orange', 'pineapple', 'grape', 'pear']
['benz', 'audi', 'byd']
['ford', 'audi', 'byd']
['byd']
H
F
['H', 'He', 'Li']
['H', 'He', 'Li']
['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne']
['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F']

Process finished with exit code 0