1.作业讲解:
写代码,有如下列表,按照要求实现每一个功能。
lis = [2,3,‘k’,[‘qwe’,20,[‘k1’,[‘tt’,3,’1’]],89],’ab’,’adv’]
1)将列表lis中的’tt’变成大写(用两种方式)。
2)将列表中的数字3变成字符串’100’(用两种方式)。
3)将列表中的字符串’1’变成数字101(用两种方式)。
‘‘‘
# lis = [2,3,‘k‘,[‘qwe‘,20,[‘k1‘,[‘tt‘,3,‘1‘]],89],‘ab‘,‘adv‘]
# lis[3][2][1][0] = ‘TT‘
# print(lis)
# lis[3][2][1][0] = lis[3][2][1][0].upper()
# print(lis)
# lis[3][2][1][1] = ‘100‘
# print(lis)
# lis[3][2][1][1] = str(lis[3][2][1][1] + 97)
# print(lis)
# lis[3][2][1][2] = int(lis[3][2][1][2]) + 100
# print(lis)
# lis[3][2][1][2] = int(lis[3][2][1][2] + ‘01‘)
# print(lis)
查找列表li中的元素,移除每个元素的空格, for i.strip()
并找出以’A’或者’a’开头,
并以’c’结尾的所有元素,
并添加到一个新列表中,最后循环打印这个新列表。
li = [‘taibai ’,’alexC’,’AbC ’,’egon’,’ Ritian’,’ Wusir’,’ aqc’]
for
‘‘‘
# li = [‘taibai ‘,‘alexC‘,‘AbC ‘,‘egon‘,‘ Ritian‘,‘ Wusir‘,‘ aqc‘]
# l1 = []
# for i in li:
# i = i.strip()
# if i[0].upper() == ‘A‘ and i[-1] == ‘c‘:
# l1.append(i)
# print(l1)
# l1 = []
# for i in li:
# i = i.strip()
# if (i.startswith(‘A‘) or i.startswith(‘a‘)) and i.endswith(‘c‘):
# l1.append(i)
# print(l1)
‘‘‘
开发敏感词语过滤程序,提示用户输入评论内容,
如果用户输入的内容中包含特殊的字符:
敏感词列表 li = ["苍老师","东京热",”武藤兰”,”波多野结衣”]
则将用户输入的内容中的敏感词汇替换成***,并添加到一个列表中;
如果用户输入的内容没有敏感词汇,则直接添加到上述的列表中
li = ["苍老师", "东京热", "武藤兰", "波多野结衣"]
l1 = []
comment = input(‘请输入‘)
for i in li:
if i in comment:
comment = comment.replace(i,‘*‘*len(i))
l1.append(comment)
print(l1)
‘‘‘
# li = [‘苍老师‘,‘东京热‘,‘武藤兰‘,‘波多野结衣‘]
# ret = input(‘input:‘) # ***东京热武藤兰波多野结衣小泽玛利亚松岛枫
# l1 = []
# for i in li:
# ret = ret.replace(i,"*"*len(i)) # ******武藤兰波多野结衣小泽玛利亚松岛枫
# l1.append(ret)
# print(l1)
# lis = []
# for i in li:
# ret = ret.replace(i,len(i) * ‘*‘)
# lis.append(ret)
# print(lis)
2.字典:增、删、改、查、嵌套
dic = {"name":"jin",
"age":18,
"sex":"male",
}
# print(dic)
#哈希表:
# print(hash(‘name‘))
# print(hash(‘fsagffsadgsdafgfdsagsadfgfag‘))
# print(hash(‘age‘))
# 增
#第一种: 有则覆盖,无则添加
# dic[‘hobby‘] = ‘girl‘
# print(dic)
# dic[‘name‘] = ‘wusir‘
# print(dic)
#第二种 setdefault 无则添加,有则不变。
# dic.setdefault(‘hobby‘)
# dic.setdefault(‘hobby‘,‘girl‘)
# dic.setdefault(‘name‘,‘ritian‘)
# print(dic)
# 删
#pop 有返回值
# print(dic.pop(‘age‘))
# print(dic)
# dic.pop(‘hobby‘) # 报错
# print(dic.pop(‘hobby‘, None)) # 返回你设定的值
#clear 清空
# dic.clear()
# print(dic)
#del
# del dic
# print(dic)
# del dic[‘name‘]
# print(dic)
# dic.popitem() # 随机删除 有返回值
# print(dic.popitem())
# print(dic)
# 改
# dic[‘name‘] = ‘太白‘
# print(dic)
#两个字典的改
# dic = {"name":"jin","age":18,"sex":"male"}
# dic2 = {"name":"alex","weight":75}
# dic2.update(dic) # 将dic键值对,覆盖并添加到dic2
# # print(dic)
# print(dic2)
# 查
# print(dic[‘name‘])
# print(dic[‘name1‘]) # 报错
# print(dic.get(‘name‘))
# print(dic.get(‘name1‘)) # 默认返回None
# print(dic.get(‘name1‘,‘咩有此键值对‘)) # 默认返回None
# 其他方法:
#keys() values() items()
# print(dic.keys(),type(dic.keys()))
# print(dic.keys())
# for i in dic.keys():
# print(i)
# for i in dic:
# print(i)
# li = list(dic.keys())
# print(li)
# print(dic.values())
# for i in dic.values():
# print(i)
# print(dic.items())
for i in dic.items():
print(i)
#特殊类型 dict 转化 成list
# print(list(dic.keys()))
#概念:分别赋值
# a,b = 2,3
# print(a,b)
# a,b = (2,3)
# print(a,b)
# a,b = [2,3]
# print(a,b)
# a = 4 ,b = 5
# a = 4
# b = 5
# a,b = b,a
# print(a,b)
# print(dic.items())
# for k,v in dic.items():
# print(k,v)
字典的嵌套
# dic = {"name_list":[‘张三‘,‘lisi‘,‘隔壁王叔叔‘],
# ‘dic2‘:{‘name‘:‘太白‘,‘age‘:12},
# }
# 1 ,给列表追加一个元素:‘旺旺‘
# l1 = dic[‘name_list‘]
# l1.append(‘旺旺‘)
# dic[‘name_list‘].append(‘旺旺‘)
# print(dic)
# ,2,给列表lisi全部大写
# print(dic[‘name_list‘][1].upper())
# dic[‘name_list‘][1] = dic[‘name_list‘][1].upper()
# print(dic)
# 3,给dic2 对应的字典添加一个键值对:hobby:girl.
# dic[‘dic2‘][‘hobby‘] = ‘girl‘
# print(dic)