3.9.2 - Lists - Adding and Removing Objects

来源:互联网 发布:网上开淘宝店流程 编辑:程序博客网 时间:2024/05/21 09:41

# Python List Examplesa = [] #Empty Listb = [0, 1, 2, 3, 4] #List with 5 itemsc = [3.14, "abc", [1, 2, 3, ["a", "b", "c"]]]  #List with a nested listd = [5, 6, 7, 8]a.append("a")a.append("b")a.append("c")a = ["a", "b", "c"]a.extend(b)len(a)a = [] #Empty Lista.append("Python")a = [] #Empty Lista.extend("Python")b.insert(0, "a") # before the index positionb.insert(3, "b")b.remove("b")  #delete by the object#an error will occur if you want to delete an inexistent elementdel d[0]  #delete by the index


结果

a = [] #Empty Listb = [0, 1, 2, 3, 4] #List with 5 itemsc = [3.14, "abc", [1, 2, 3, ["a", "b", "c"]]]  #List with a nested listd = [5, 6, 7, 8]a.append("a")a.append("b")a.append("c")a = ["a", "b", "c"]a.extend(b)a// Result: ['a', 'b', 'c', 0, 1, 2, 3, 4] //len(a)// Result: 8 //a = [] #Empty Lista.append("Python")a// Result: ['Python'] //a = [] #Empty Lista.extend("Python")a// Result: ['P', 'y', 't', 'h', 'o', 'n'] //b.insert(0, "a") # before the index positionb// Result: ['a', 0, 1, 2, 3, 4] //b.insert(3, "b")b// Result: ['a', 0, 1, 'b', 2, 3, 4] //b.remove("b")  #delete by the objectb// Result: ['a', 0, 1, 2, 3, 4] //d// Result: [5, 6, 7, 8] //del d[0]  #delete by the indexd// Result: [6, 7, 8] //






0 0