python找出列表中的奇数以及前拷贝和深拷贝的不同

# -*- coding:utf-8 -*-
'''
找出列表中的奇数以及前拷贝和深拷贝的不同
@author:cnliutz
'''
lst = [1,2,4,3,5]
for x in lst:
    if x % 2 == 0:
        lst.remove(x)   # remove()方法会改变列表的长度,导致循环次数减少
print(lst)

lst = [1,2,4,3,5]   # 使用迭代器            
for x in iter(lst):
    if x % 2 == 0:
        lst.remove(x)
print(lst)      

#正确做法是
# lst = [1,2,4,3,5]
for x in lst[:]:    #(浅拷贝) 使用切片,复制列表
    if x % 2 == 0:
        lst.remove(x)   
print(lst)
#----------
#如果是字符串,替换其中的元音字母,可以使用replace()方法
s = "beautiful" #字符串是不可变的,不能直接修改,需要使用replace()方法
for ch in s:
    if ch in "aeiou":
        s=s.replace(ch,"")
print(s)

s = 'hello world'
s = s.replace('a','*').replace('e','*').replace('i','*').replace('o','*').replace('u','*')
print(s)

#如果是字典,删除其中的某个键值对,可以使用pop()方法    
d = {'a':1,'b':2,'c':3}
d.pop('a')
print(d)

#浅拷贝:使用切片,复制列表
#深拷贝:使用deepcopy()方法,复制列表
#浅拷贝和深拷贝的区别:前拷贝只能复制一层,深拷贝可以复制多层
#浅拷贝
lst = [1,2,3,[4,5,6]]
lst2 = lst[:]
lst2[3][0] = 7
print(lst)
print(lst2)
#lst、lst1两者结果相同
#深拷贝
import copy
#dir
lst = [1,2,3,[4,5,6]]
lst2 = copy.deepcopy(lst)
lst2[3][0] = 7
print(lst)
print(lst2)
#lst、lst1两者结果不同