2026-05-07 18:05:22
核心思路:通过切片操作实现列表的浅拷贝。关键点:
代码实现:
input_list = [3, 2, '1', [1, 2]]print('original list: {}'.format(input_list))copyed_list = input_list[:] # 切片浅拷贝print('copyed_list: {}'.format(copyed_list))输出结果:
original list: [3, 2, '1', [1, 2]]copyed_list: [3, 2, '1', [1, 2]]替代方法:若需深拷贝(完全独立),可使用 copy.deepcopy():
import copydeep_copied_list = copy.deepcopy(input_list)Example-8:打印乘法口诀核心思路:通过双重循环控制行列,格式化输出对齐。关键点:
方法一:基础循环实现
def multiplication_table1(): for i in range(1, 10): for j in range(1, i + 1): print('%d*%d=%-2d ' % (j, i, i * j), end='') # 调整j和i顺序为常规口诀 print('')方法二:一行代码实现(列表推导式)
def multiplication_table2(): print('n'.join([' '.join(['%s*%s=%-2s' % (y, x, x * y) for y in range(1, x + 1)]) for x in range(1, 10)]))输出结果(以方法一为例):
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 ...1*9=9 2*9=18 ... 9*9=81注意事项:
代码参考:完整示例见