列表
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
print("lst1:{}".format(lst1))
print("lst2:{}".format(lst2))
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
lst1.append(lst2)
print("lst1.append(lst2):{}".format(lst1))
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
lst1.extend(lst2)
print("lst1.extend(lst2):{}".format(lst1))
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
lst1.insert(0, "abc")
print("lst1.insert(0,\"abc\"):{}".format(lst1))
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
print("lst1 + lst2 = {}".format(lst1 + lst2))
print("lst2 * 2 = {}".format(lst2 * 2))
lst1 = [1, 2, 3, 4, 5]
print("lst1.clear():{}".format(lst1.clear()))
lst1 = [1, 2, 3, 4, 5]
lst1.remove(5)
print("lst1.remove(5):{}".format(lst1))
lst1 = [1, 2, 3, 4, 5]
a = lst1.pop(3)
print("lst1.pop(3):{},{}".format(lst1, a))
b = lst1.pop()
print("lst1.pop():{},{}".format(lst1, b))
lst1 = [1, 2, 3, 4, 5]
lst1.reverse()
print("lst1.reverse():{}".format(lst1))
lst3 = [3, 2, 5, 1, 4]
lst3.sort(key=None, reverse=False)
print("lst3.sort(key=None,reverse=False):{}".format(lst3))
lst3 = [3, 2, 5, 1, 4]
lst1.sort(key=None, reverse=True)
print("lst3.sort(key=None,reverse=True):{}".format(lst3))
lst4 = [1, 2, 3, 3, 3, 4, 5]
print("lst4.count(3):{}".format(lst4.count(3)))
lst4 = [1, 2, 3, 3, 3, 4, 5]
print("lst4[2:5]:{}".format(lst4[2:5]))
lst4 = [1, 2, 3, 3, 3, 4, 5]
print("lst4.index(3):{}".format(lst4.index(3)))
---------------------------------------
lst1:[1, 2, 3, 4, 5]
lst2:['x', 'y', 'z']
lst1.append(lst2):[1, 2, 3, 4, 5, ['x', 'y', 'z']]
lst1.extend(lst2):[1, 2, 3, 4, 5, 'x', 'y', 'z']
lst1.insert(0,"abc"):['abc', 1, 2, 3, 4, 5]
lst1 + lst2 = [1, 2, 3, 4, 5, 'x', 'y', 'z']
lst2 * 2 = ['x', 'y', 'z', 'x', 'y', 'z']
lst1.clear():None
lst1.remove(5):[1, 2, 3, 4]
lst1.pop(3):[1, 2, 3, 5],4
lst1.pop():[1, 2, 3],5
lst1.reverse():[5, 4, 3, 2, 1]
lst3.sort(key=None,reverse=False):[1, 2, 3, 4, 5]
lst3.sort(key=None,reverse=True):[3, 2, 5, 1, 4]
lst4.count(3):3
lst4[2:5]:[3, 3, 3]
lst4.index(3):2
元组
tup1 = (1, 2, 3, 4, 5, 6)
print("tup1.index(3):{}".format(tup1.index(3)))
print("tup1[1:5]:{}".format(tup1[1:5]))
-------------------------------
tup1.index(3):2
tup1[1:5]:(2, 3, 4, 5)
拷贝
print("~~~lst1=lst.copy()~~~")
lst = [1, 2, 3, 4, ["x", "y", "z"]]
lst1 = lst.copy()
print("lst:{}".format(lst))
print("lst1:{}".format(lst1))
lst[4][0] = "a"
lst.insert(0, 7)
print("after changes lst:{}".format(lst))
print("after changes lst1:{}".format(lst1))
print("~~~lst1 = copy.copy(lst)~~~")
lst = [1, 2, 3, 4, ["x", "y", "z"]]
lst1 = copy.copy(lst)
print("lst:{}".format(lst))
print("lst1:{}".format(lst1))
lst[4][0] = "a"
lst.insert(0, 7)
print("after changes lst:{}".format(lst))
print("after changes lst1:{}".format(lst1))
print("~~~lst1 = copy.deepcopy(lst)~~~")
lst = [1, 2, 3, 4, ["x", "y", "z"]]
lst1 = copy.deepcopy(lst)
print("lst:{}".format(lst))
print("lst1:{}".format(lst1))
lst[4][0] = "a"
lst.insert(0, 7)
print("after changes lst:{}".format(lst))
print("after changes lst1:{}".format(lst1))
-------------------------------------
~~~lst1=lst.copy()~~~
lst:[1, 2, 3, 4, ['x', 'y', 'z']]
lst1:[1, 2, 3, 4, ['x', 'y', 'z']]
after changes lst:[7, 1, 2, 3, 4, ['a', 'y', 'z']]
after changes lst1:[1, 2, 3, 4, ['a', 'y', 'z']]
~~~lst1 = copy.copy(lst)~~~
lst:[1, 2, 3, 4, ['x', 'y', 'z']]
lst1:[1, 2, 3, 4, ['x', 'y', 'z']]
after changes lst:[7, 1, 2, 3, 4, ['a', 'y', 'z']]
after changes lst1:[1, 2, 3, 4, ['a', 'y', 'z']]
~~~lst1 = copy.deepcopy(lst)~~~
lst:[1, 2, 3, 4, ['x', 'y', 'z']]
lst1:[1, 2, 3, 4, ['x', 'y', 'z']]
after changes lst:[7, 1, 2, 3, 4, ['a', 'y', 'z']]
after changes lst1:[1, 2, 3, 4, ['x', 'y', 'z']]