标签:ever remove 插入 浅拷贝 st3 join() 获取 copy append
2018-12-27
list[index] = new
list1 = ["saj","saf","safgt","th",["saf","dsg"]] list2 = ["saj","saf","safgt","th",["saf","dsg"]] list3 = ["saj","saf","safgt","th",["saf","dsg"]] list1[1] = 120 list1[2:4] = "12" list2[2:4] = [1,2] list3[2:4] = [12] print(list1) print(list2) print(list3) 结果为: [‘saj‘, 120, ‘1‘, ‘2‘, [‘saf‘, ‘dsg‘]] [‘saj‘, ‘saf‘, 1, 2, [‘saf‘, ‘dsg‘]] [‘saj‘, ‘saf‘, 12, [‘saf‘, ‘dsg‘]]
列表元素的删除:
(1)索引:del list[index]
(2)切片:del list[index:index]
(3)根据索引删除某个值,并获取其值,若无指定值,则删除最后一个元素: list.pop(index)
(4)根据值来删除某个值,若有相同的值,删除第一个:list.remove(元素值)
列表中的元素以逗号为整体,看大的
list[index][index][index]........层层递进
list("safaghth")
利用for循环一个一个处理,在既有数字又有字符串时,把数字强制转换为字符串
list = ["LYN","WCB",596,922] s = "" for i in list: s = s + str(i) print(s) 结果为: LYNWCB596922
若只有字符串时,可利用str.join(),来进行拼接
list = ["LYN","WCB"] a = "" print(a.join(list)) 结果为: LYNWCB
list.append(整型或列表或字符串等等)
list.extend() = for i in 增量:
list.append(i)
a = [123,156,"sfsa",156,"sfmm"] b = [123,156,"sfsa",156,"sfmm"] x = [123,54] a.append(x) b.extend(x) print(a) print(b) 结果为: [123, 156, ‘sfsa‘, 156, ‘sfmm‘, [123, 54]] [123, 156, ‘sfsa‘, 156, ‘sfmm‘, 123, 54]
list.clear()
new_list = list.copy()
list.count(元素)
list.insert(index,元素值)
list.reverse()
list.sort(reverse = True):从大到小
list.sort():从小到大
2018-12-27 13:53:16
标签:ever remove 插入 浅拷贝 st3 join() 获取 copy append
原文地址:https://www.cnblogs.com/LynWcb596922/p/10183851.html