排序算法
#排序算法
#内置排序,a.sort()节省内存; sorted(a)数据可以再次调用
'''a = [1,2,2,3,4,6,2]
b = sorted(a)
print(b)
print(id(a))
print(id(b))'''
#冒泡排序
#简单冒泡
'''a = [4,2,6,18,9]
for i in range(len(a)-1):
for j in range(i+1,len(a),1):#循环起始是i+1
if a[i]>a[j]:
temp = a[i]
a[i] = a[j]
a[j] = temp
del temp#删除temp地址
print(a)'''
#常规冒泡,比较相邻位置的大小
'''a = [2,3,2,7,4,3]
for i in range(len(a)-1):
for j in range(len(a)-1-i):
if a[j] a[j]:
min_index = j
if min_index !=i:
temp = a[i]
a[i] = a[min_index]
a[min_index] = temp
print(a)'''
列表操作
#面试常见问题:垃圾回收机制
#python内存模型
#首先字符串转列表
'''a = "12345"
b = [str(i)for i in a]
print(a)'''
#追加
names = ['a','a','b','c','d']
'''
names.append("e")
print(names)'''
#清除
'''names.clear()
print(names)'''
#删除
'''names.pop()
print(names)
names.remove("c")
print(names)
#查找元素所在位置
index= names.index("b",1,3)
print(index)
index =names.index("d")
print(index)
#统计字符串
count = names.count("a")
print(count)
#插入
names.insert(2,"t")
#拼接列表
p = [1,2,3]
names.extend(p)
print(names)
print(p)'''
##切片
'''names = ['a','a','b','c','d']
#列出所有元素
print(names[::])
#列出最后一个元素
print(names[-1])'''
#列出片段
'''print(names[1:4])
print(names[::])
print(names[::2])
print(names[::-1])'''
#复制copy
'''b = names.copy()
print(b)
print(names)'''
#?浅表复制?
'''import copy
a = ["f","b","c",[1,2]]
b = copy.copy(a)
print(b)
a[3].append(3)#append能操作列表类型
print(a)
print(b)'''
#
'''import copy
b = copy.copy(names)
b[1] = "o"
print(b)
print(names)'''
'''import copy
a = ["a","b",[1,2]]
b = copy.copy(a)
a[2].append(3)
print(a)
print(b)'''