1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| # 示例 >>> a = [1,2,3,'a','b','c'] >>> a [1, 2, 3, 'a', 'b', 'c'] >>> a[2] = "x" # 注:修改元素 >>> a [1, 2, 'x', 'a', 'b', 'c'] >>> a[1:1] # 注:[1,1) [] # 注:为空 >>> a[1:1] = "abc" # 注:在起始位置 索引为1 处之前 增加列表 str类型转换为列表 >>> a [1, 'a', 'b', 'c', 2, 'x', 'a', 'b', 'c'] >>> b = ["a","b","c"] >>> b[2:2] = ["x","y"] # 注:索引为2处之前 增加列表 >>> b ['a', 'b', 'x', 'y', 'c'] >>> b[2:4] = ["x","y","z"] # 注:索引2、3 的值 替换 >>> b ['a', 'b', 'x', 'y', 'z', 'c'] >>> b[1:5] = "12" # 注:切出来后,再转换为列表。str类型转换为列表 >>> b # 注:索引1、2、3、4 的值 替换 为 ‘1’‘2’ ['a', '1', '2', 'c'] >>> b ['a', '1', '2', 'c'] >>> b[5:-3] [] >>> b[3:-3] # 注:获取不到 [] >>> b[3:-3] = "xy" # 注:这个位置开始(start位置开始),替换成其他的list >>> b # 注:看 找出的元素是否为空 ['a', '1', '2', 'x', 'y', 'c'] >>> c = ["a","b","c","d","1"] >>> c[1:4] = "hello" # 注:找出b c d 把bcd 替换成h e l l o >>> c ['a', 'h', 'e', 'l', 'l', 'o', '1'] >>> c = ['a', 'h', 'e', 'l', 'l', 'o', '1'] >>> c[5:3] # 注:获取不到 [] >>> c[5:3] = ["x","y"] # 注:c[5:3]为空,在 o (下标5)处插入x y >>> c ['a', 'h', 'e', 'l', 'l', 'x', 'y', 'o', '1']
|