1、 有两个列表
l1 = [11,22,33]
l2 = [22,33,44]
a. 获取内容相同的元素列表
for i in l1: if i in l2: print(i)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
22
33
b. 获取 l1 中有,l2 中没有的元素列表
for i in l1: if i not in l2: print(i)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
11
c. 获取 l2 中有, l1 中没有的元素列表
for i in l2: if i not in l1: print(i)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
44
d. 获取 l1 和 l2 中内容都不同的元素
for i in l1: if i not in l2: print(i) for i in l2: if i not in l1: print(i)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
11
44
2、 用代码实现,利用下划线将列表的每一个元素拼接成字符串,li = ['alex','eric','Witharush']
li = ['alex','eric','Witharush'] v = "_".join(li) print(v)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
alex_eric_Witharush
若列表里面有数字
li = ['alex','eric','Witharush',123] li [3] = str(li[3]) print("_".join((li)))
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
alex_eric_Witharush_123
若先定义一个空的字符串,通过for循环拼接
s = "" li = ['alex','eric','Witharush',123] for i in li: s = s + str(i) print(s) print("_".join(s))
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
alexericWitharush123
a_l_e_x_e_r_i_c_W_i_t_h_a_r_u_s_h_1_2_3
3、有以下列表:
nums = [2,7,11,15,1,8,7]
请找到列表中任意两个元素相加能够等于9的元素组合,如, [(0,1),(4,5)]
way 1:通过printf的性质,输出样式为:(x,y)
for i in range(0,len(nums)): for j in range(0,len(nums)): if nums[i] + nums[j] == 9: print("("+str(i) + "," + str(j)+")",end="\t")
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
(0,1) (0,6) (1,0) (4,5) (5,4) (6,0)
way 2:通过先定义一个空的列表,用for循环和append方法将每种组合追加到空列表中
a = [] for i in range(0,len(nums)): for j in range(0,len(nums)): if nums[i] + nums[j] == 9: a.append((i,j)) print(a)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
[(0, 1), (0, 6), (1, 0), (4, 5), (5, 4), (6, 0)]
4、写代码,有如下列表,按照要求实现每一个功能
li = ["Witharush","eric","rain"]
a.计算列表长度并输出
print(len(li))
b.列表中追加元素"seven",并输出添加后的列表
li.append("seven") print(li) # 如果用 print(li.append("seven")) 输出为 None
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
['Witharush', 'eric', 'rain', 'seven']
c.请在列表的第1个位li请在列表的第一个位置插入元素"Tony",并输出添加后的列表
li.insert(0,"Tony") print(li)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
['Tony', 'Witharush', 'eric', 'rain']
d.请修改列表的第2个位置插入元素"Kelly",并输出添加后的列表
li[1] = "Kelly" print(li)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
['Witharush', 'Kelly', 'rain']
e.请删除列表中的元素"eric",并输出修改后的列表
li.remove("eric") print(li)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
['Witharush', 'rain']
f.请删除列表中的第2个元素,并输出删除的元素的值和删除元素后的列表
v = li.pop(1) print(v) print(li)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
eric
['Witharush', 'rain']
g.请删除列表中的第3个元素,并输出删除元素后的列表
li.pop(2) print(li)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
['Witharush', 'eric']
h.请删除列表中的第2至4个元素,并输出删除元素后的列表
del li[1:5] print(li)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
['Witharush']
i.请将列表所有的元素反转,并输出反转后的列表
li.reverse() print(li) # 入如果用 print(li.reverse()) 输出为 None
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
['rain', 'eric', 'Witharush']
j.请使用for,len,range输出列表的索引
for i in range(len(li)): print(i,li[i])
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
0 Witharush
1 eric
2 rain
k.请使用enumrate是输出列表元素和序号(序号从100开始)
for i , v in enumerate(li, 100): print(i,v)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
100 Witharush
101 eric
102 rain
l.请使用for循环输出列表的所有元素
for i in li : print(i)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
Witharush
eric
rain
5、写代码,有如下元祖,按照要求实现每一个功能
tu = ('alex','eric','Witharush')
a.计算元祖长度并输出
print(len(tu))
b.获取元祖的第二个元素,并输出
print(tu[1])
c.获取元祖的第 1-2 个元素,并输出
print(tu[0:2])
d.请使用for输出元祖的元素
for i in tu:
print(i)
e.请使用for,len,range输出元素的索引
for i in range(0,len(tu)): print(i,tu[i])
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
0 alex
1 eric
2 Witharush
f.请使用enumrate输出元祖元素和序号(序号从10开始)
for i, v in enumerate(tu, 10): print(i,v)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
10 alex
11 eric
12 Witharush
6、有如下变量,请实现要求的功能
tu = ("Witharush",[11,22,{"k1":'v1',"k2":["age","name"],"k3":(11,22,33)},44])
a.讲述元祖的特性
一级元素不可被修改,不能被增加或者删除
b.请问tu变量中的第一个元素"Witharush"是否可以被修改?
不行
c.请问tu变量中的"k2"对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素"Seven"
列表;可以被修改
way 1:
tu[1][2]["k2"]="age","name","Seven" print(tu)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
('Witharush', [11, 22, {'k1': 'v1', 'k2': ('age', 'name', 'Seven'), 'k3': (11, 22, 33)}, 44])
way 2:
tu[1][2]["k2"].append("Seven") print(tu)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
('Witharush', [11, 22, {'k1': 'v1', 'k2': ['age', 'name', 'Seven'], 'k3': (11, 22, 33)}, 44])
d.请问tu变量中的"k3"对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素"Seven"
元祖; 元祖不能修改,但是可以将元祖里的值重新用字符串来替换
tu[1][2]["k3"] =(11,22,33,"Seven") print(tu)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
('Witharush', [11, 22, {'k1': 'v1', 'k2': ['age', 'name'], 'k3': (11, 22, 33, 'Seven')}, 44])
若用append,报错
tu[1][2]["k3"].append("Seven") print(tu)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
Traceback (most recent call last):
File "D:/PyProject/day13_homework/text.py", line 152, in <module>
tu[1][2]["k3"].append("Seven")
AttributeError: 'tuple' object has no attribute 'append'
7、用1,2,3,4,5,6,7,8 ,8个数,能组成多少个互不相同且无重复的两位数?
way 1 :
count = 0 for i in range(1, 9): for v in range(2,9): #每次一大循环都有一个重复的两位数,可以直接让第二轮循环比第一轮少循环一次 (野路子) count += 1 print(count)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
56
way 2 :
count = 0 for i in range(1, 9): #都从1到8开始 如果两位数一样了就不加 (常规) for v in range(1,9): if i != v: count += 1 print(count)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
56
way 3:
count = 0 #比较死板,不灵活 for i in range(1, 9): for i in range(1,9): count += 1 count = count -8 print(count)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
56
8、九九乘法表
for i in range(1,10): for v in range(1,i+1): print(str(i) + '*' + str(v) + '=' + str(i*v),end="\t") print("\n",end="")
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
9、print用法中的“sep” 、"end"
print("Witharush","真","帅") print("Witharush","真","帅",sep='帅',end="\t") # sep:一次性输出多个时,默认分隔为空格,不用end的话,默认执行print("\n") print("Witharush","真","帅",sep='帅')
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
Witharush 真 帅
Witharush帅真帅帅 Witharush帅真帅帅
10、分页实现内容
a.通过for循环创建301条数据,数据类型不限,如:
Witharush-1 Witharush1@live.com pwd1
Witharush-2 Witharush2@live.com pwd2
Witharush-3 Witharush3@live.com pwd3
...
提示用户 请输入要查看的页码,当用户输出指定页码,也显示指定数据
注意:
每页显示10条数据
用户输出页码是非十进制数字,也提示输入内容格式错误
while True: user_list = [ ] for i in range(1,302): temp = {"name":"Witharush"+str(i),"email":"Witharush"+str(i)+"@live.com","password":"pwd"+str(i) } user_list.append(temp) s = input("请输入要查看的页码:") s = int(s) #1 >>> (0,10) #2 >>>(10,20) #3 >>>(20,30) start = (s-1) * 10 end = s * 10 result = user_list[start:end] for item in result: print(item,type(item))
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
请输入要查看的页码:1
{'name': 'Witharush1', 'email': 'Witharush1@live.com', 'password': 'pwd1'} <class 'dict'>
{'name': 'Witharush2', 'email': 'Witharush2@live.com', 'password': 'pwd2'} <class 'dict'>
{'name': 'Witharush3', 'email': 'Witharush3@live.com', 'password': 'pwd3'} <class 'dict'>
{'name': 'Witharush4', 'email': 'Witharush4@live.com', 'password': 'pwd4'} <class 'dict'>
{'name': 'Witharush5', 'email': 'Witharush5@live.com', 'password': 'pwd5'} <class 'dict'>
{'name': 'Witharush6', 'email': 'Witharush6@live.com', 'password': 'pwd6'} <class 'dict'>
{'name': 'Witharush7', 'email': 'Witharush7@live.com', 'password': 'pwd7'} <class 'dict'>
{'name': 'Witharush8', 'email': 'Witharush8@live.com', 'password': 'pwd8'} <class 'dict'>
{'name': 'Witharush9', 'email': 'Witharush9@live.com', 'password': 'pwd9'} <class 'dict'>
{'name': 'Witharush10', 'email': 'Witharush10@live.com', 'password': 'pwd10'} <class 'dict'>
11、用python开发程序自动计算方案
公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买100只鸡,其
中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100问钱?
for gj in range(1,21): for mj in range(1,100//3): for xj in range(1,101): if gj + mj + xj == 100 and 5 * gj + 3 * mj + xj/3 ==100: print(gj,mj,xj)
D:\PyProject\day13_homework\venv\Scripts\python.exe D:/PyProject/day13_homework/text.py
4 18 78
8 11 81
12 4 84