学习测试开发的Day74,真棒!
学习时间为1H
第四章列表、元组、字典-习题(11-13)
11、求两个集合的交集和并集
代码
list1=[1,2,3,4]
list2=[2,3,5,5]
def jiaoji(L1,L2):
listj=[]
listb=[]
for i in L1:
listb.append(i)
if i in L2:
listj.append(i)
for j in L2:
listb.append(j)
return set(listb),listj
print(jiaoji(list1,list2))
结果
PS D:\0grory\chapter4> python .\jiaoandbing.py
({1, 2, 3, 4, 5}, [2, 3])
PS D:\0grory\chapter4>
12、一个包含多个数字的列表,请使用随机的方式,将每个数字+1后,生成新列表
代码:
import random
list1=[7,9,1,0]
len_list1=len(list1)
result=[]
while 1:
i=random.randint(0,len_list1-1)
if list1[i]+1 not in result:
result.append(list1[i]+1)
if len(result)==4:
break
print(result)
结果:
PS D:\0grory\chapter4> python .\random1.py
[1, 10, 2, 8]
13、判断一个字符串是否为回文字符串
所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的。比如"level" 、 “aaabbaaa”
代码:
def huishu(list1):
flag=1
len_list1=len(list1)
l=int((len_list1)/2)
for i in range(l):
#print(list1[i])
#print(list1[len_list1-i-1])
if list1[i]!=list1[len_list1-i-1]:
flag=0
break
else:
flag=1
if flag==1:
return True
else:
return False
list2="aaabbaaa"
print(huishu(list2))
print(huishu("asdfsdf"))
print(huishu("level"))
print(huishu("level1"))
结果:
PS D:\0grory\chapter4> python .\huishu.py
True
False
True
False