标签:pytho utf-8 bsp push 报错 迭代 没有 span xtend
# 列表类似JS中的数组 lst = [1,2,3,4,5] # 列表常用的一些API lst.append(6) # 类比JS中的push,在列表后面追加一项元素 lst.insert(1,‘xiaT‘) print(lst) # 在索引为1的位置添加元素 lst2 = [1,2,3] lst3 = [4,5,6] lst2.extend(lst3) print(lst2) # 这个是迭代添加 lst4 = [1,2,3] lst4.pop() print(lst4) # 删除最后一项,也可以制定索引删除会返回删除项 lst = ["UZI","XIAOHU","MLXG","MING"] result = lst.remove("UZI") print(result) print(lst) # removeAPI返回的是None它内部实现没有做对象返回这里是单删除指定元素 lst.clear() print(lst) # 这个就不多说了清空 lst = ["UZI","XIAOHU","MLXG","MING"] del lst[1:3:2] print(lst) # # 这里是切片删除步长2的情况下只能切掉XIAOHU # # 下面介绍下修改 lst = ["UZI","XIAOHU","MLXG","MING"] lst[0:3:2] = ["SMLZ","FAKER"] print(lst) # # 也可以使用步长迭代修改,但要注意的是如果元素个数对不上会报错 lst = ["UZI","XIAOHU","MLXG","MING"] lst[0:1] = "简单实用的方法试试" print(lst) # [‘\xe7‘, ‘\xae‘, ‘\x80‘, ‘\xe5‘, ‘\x8d‘, ‘\x95‘, ‘\xe5‘, ‘\xae‘, ‘\x9e‘, ‘\xe7‘, ‘\x94‘, ‘\xa8‘, ‘\xe7‘, ‘\x9a‘, ‘\x84‘, ‘\xe6‘, ‘\x96‘, ‘\xb9‘, ‘\xe6‘, ‘\xb3‘, ‘\x95‘, ‘\xe8‘, ‘\xaf‘, ‘\x95‘, ‘\xe8‘, ‘\xaf‘, ‘\x95‘, ‘XIAOHU‘, ‘MLXG‘, ‘MING‘] # 返回的是这堆东西,我这里是utf-8的字节码,正常是汉字,也就他将字符串迭代扩展到了0的位置 lst = ["UZI","XIAOHU","MLXG","MING"] lst[0:1:2] = [1,2,3,4,5,6] print(lst) # 我们发现数组也是同理但添加步长后就必须一一对应否则报错,字符串也是同理 # # 其他操作 lst = ["UZI","UZI","XIAOHU","MLXG","MING"] num = lst.count("UZI") print(num) # # 查询UZI出现的次数 # lst = [8,7,6,1,2,3,45] lst.sort() print(lst) lst.sort(reverse=True) print(lst) # # 这个跟JS差不多升序降序排列 # lst = ["UZI","UZI","XIAOHU","MLXG","MING"] lst.reverse() print(lst) # # 反转很简单
标签:pytho utf-8 bsp push 报错 迭代 没有 span xtend
原文地址:https://www.cnblogs.com/tengx/p/11667225.html