#author("2023-09-26T14:20:53+08:00","default:Admin","Admin") #author("2023-11-10T19:19:43+08:00","default:Admin","Admin") [[Python]] &color(red){※This article is based on Python 3.7.3}; #contents * 基本数组 [#q3a4b657] 定义 arr = [] 增加元素 arr.append('aaa') 删除数组里的指定元素 del arr[0] * 切片 [#z2d8dcce] Python中的切片(slice)操作可以用来获取数组中的一部分数据。对于一个数组a,我们可以使用a[:n]来获取前n个数据。下面是一个示例代码: #codeprettify{{ a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] n = 5 print(a[:n]) }} *列表解析 [#se5638b6] 列表解析(list comprehension)是一种简洁的方式来创建新的列表。我们可以在列表解析中使用条件语句来筛选数组中的数据,并输出前n个数据。下面是一个示例代码: #codeprettify{{ a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] n = 5 output = [x for x in a if x <= n] print(output) }} * 遍历 [#x5e284b9] #codeprettify{{ for x in [1,2,3] print(x) 输出: 1 2 3 }} #hr(); コメント: #comment_kcaptcha