当前位置:网站首页>day7-列表作业(1)
day7-列表作业(1)
2022-08-05 05:13:00 【非鱼丶丶】
- 创建一个列表,列表中有10个数字, 保证列表中元素的顺序,对列表进行排重,并对列表使用进行降序排序
例如:[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
--- 去重之后 [70, 88, 91, 107, 234, 177, 282, 197]
---- 降序排序 [282, 234, 197, 177, 107, 91, 88, 70]
num = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
nums = []
for x in num:
if x not in nums:
nums.append(x)
nums.sort(reverse=True)
print(nums)
- 利用列表推导式, 完成以下需求
a. 生成一个存放1-100中各位数为3的数据列表
结果为 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
num = [x for x in range(100) if x % 10 == 3]
print(num)
b. 利用列表推到是将 列表中的整数提取出来
例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
num = [True, 17, "hello", "bye", 98, 34, 21]
nums = []
for x in num:
if type(x) == int:
nums.append(x)
print(nums)
c.利用列表推导式 存放指定列表中字符串的长度
例如: ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
num = ["good", "nice", "see you", "bye"]
nums = []
for x in num:
c = len(x)
nums.append(c)
print(nums)
d. 利用列表推导式删除列表中整数个位数小于5的元素
例如:[24, 'abc', 99, True, 21, 38, 'hello'] --- ['abc', 99, True, 38, 'hello']
num = [24, 'abc', 99, True, 21, 38, 'hello']
for x in num[:]:
if type(x) == int and x % 10 < 5:
num.remove(x)
print(num)
e. 利用列表推导式获取元素是元组的列表中每个元组的最后一个元素
例如:[(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)] --- [30, 'hello', 3.4, False]
f.利用列表推导式将数字列表中所有的奇数乘以2,所有的偶数除以2
例如: [23, 4, 67, 88, 90, 21] -> [46, 2, 134, 44, 45, 42]
num = [23, 4, 67, 88, 90, 21]
nums = []
for x in num:
if x % 2:
a = x * 2
nums.append(a)
else:
b = x // 2
nums.append(b)
print(nums)
已知一个列表获取列表中指定元素所有的下标
例如:[10, 20, 34, 10, 9, 78] 10的下标:[0, 3] 20的下标:[1] 30的下标:[]
num = [10, 20, 34, 10, 9, 78] a = int(input('输入:')) for x, y in enumerate(num): if a == y: print(x)
*已知一个数字列表,写程序判断这个列表时候是连续递增列表。
例如: [1, 2, 3, 4, 5] -> True [23, 45, 78, 90] -> True [1, 3, 2, 4, 5] -> False
num = [10, 20, 34, 10, 9, 78] nums = sorted(num) if nums == num: print(True) else: print(False)
已知两个列表,将两个列表按照下面的规律交叉合并
A = [10, 20, 30, 40, 50] B = [100, 200, 300] 结果:[10, 100, 20, 200, 30, 300, 40, 50]
A = [10, 20, 30, 40, 50] B = [100, 200, 300] num = [] while True: a = A.pop(0) b = B.pop(0) num.append(a) num.append(b) if A == [] or B == []: break num += A + B print(num)
已知两个有序列表,将两个列表合并,合并后的新列表中元素仍然是递增列表
A = [10, 20, 30, 40, 50] B = [25, 44, 60] 结果:[10, 20, 25, 30, 40, 45, 50, 60]
A = [10, 20, 30, 40, 50] B = [25, 44, 60] num = A + B num.sort() print(num)
边栏推荐
- Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
- 物理层的接口有哪几个方面的特性?各包含些什么内容?
- RL reinforcement learning summary (1)
- 【记一下1】2022年6月29日 哥和弟 双重痛苦
- [Decoding tools] Some online tools for Bitcoin
- Requests the library deployment and common function
- Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction
- 结构光三维重建(二)线结构光三维重建
- 第四讲 back propagation 反向传播
- redis事务
猜你喜欢
第二讲 Linear Model 线性模型
Dephi reverse tool Dede exports function name MAP and imports it into IDA
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
Lecture 2 Linear Model Linear Model
Flutter学习2-dart学习
Flutter真机运行及模拟器运行
【过一下 17】pytorch 改写 keras
Flutter real machine running and simulator running
Excel Paint
随机推荐
[Decoding tools] Some online tools for Bitcoin
What field type of MySQL database table has the largest storage length?
物理层的接口有哪几个方面的特性?各包含些什么内容?
The underlying mechanism of the class
机器学习(二) —— 机器学习基础
小白一枚各位大牛轻虐虐
How to quickly upgrade your Taobao account to a higher level
Lecture 2 Linear Model Linear Model
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
The role of DataContext in WPF
What are the characteristics of the interface of the physical layer?What does each contain?
vscode+pytorch use experience record (personal record + irregular update)
Database experiment five backup and recovery
How can Flutter parent and child components receive click events
RDD和DataFrame和Dataset
pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
RL强化学习总结(一)
入口点注入
The difference between span tag and p