【每日一练】python列表

devtools/2024/9/23 4:22:05/

1、输入一个整数列表,将列表中的元素按照逆序输出。

python">list1=[5,4,5,6]
list1.reverse()
print(list1)
[6, 5, 4, 5]

2、输入一个字符串列表,输出其中长度大于等于5的字符串,并且将它们转换为大写形式。

python">list1=['hello','lol','ak47','aliang']
for i in list1:if len(i) >=5:print(i.upper())
HELLO
ALIANG

3、输入一个整数列表,输出该列表中所有奇数的平方和。

python">list1=[1,2,3,4,5,6,7,8,9]
sum = 0
for num in list1:if num % 2 == 1:sum += num ** 2
print(sum)
165

4、输入一个列表,输出该列表中所有正整数的和。

python">list1=[-5,-1,0,1,2,3,1.7,'i']
sum = 0
for num in list1:if isinstance(num, (int)) and num > 0 :sum+= num
print(sum)
6

5、输入一个列表,输出该列表中所有偶数的平均值。

python">list1=[-5,-1,0,1,2,3,1.7,'i']
sum = 0
i = 0
for num in list1:if isinstance(num, (int)) and num % 2 == 0 :sum+= numi+=1
print(sum/i)
1.0

6、已知一个数字列表,求所有元素和。

python">lst = [51, 54, 85, 45, 80, 45, 12, 96, 789, 45, 69]
python">lst = [51, 54, 85, 45, 80, 45, 12, 96, 789, 45, 69]
sum = 0
for numb in lst:sum += numb
print(sum)
1371

7、已知一个数字列表,输出所有奇数下标元素。

python">lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
python">lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
for j in range(len(lst)):if j % 2 == 1:print(lst[j])
54
45
45
96
45

8、已知一个数字列表,输出所有元素中,值为奇数的元素。

python">lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
python">lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
for i in lst:if i % 2 == 1:print(i)
59
89
45
45
789
45
69

9、已知一个数字列表,将所有元素乘二。

python">nums = [1, 2, 3, 4]
python">nums = [1, 2, 3, 4]
for j in range(len(nums)):nums[j] = nums[j] * 2
print(nums)
[2, 4, 6, 8]

10、有一个长度是10的列表,数组内有10个人名,要求去掉重复的

例如:names = [‘张三’, ‘李四’, ‘大黄’, ‘张三’] -> names = [‘张三’, ‘李四’, ‘大黄’]

python">names = ['张三', '李四', '大黄', '张三','小黑','小白','小明','小红']
python">names = ['张三', '李四', '大黄', '张三', '小黑', '小白', '小明', '小红']
names_unique = []
for name in names:if name not in names_unique:names_unique.append(name)print(names_unique)
['张三', '李四', '大黄', '小黑', '小白', '小明', '小红']

11、有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)

python">nums = [19, 89, 90, 600, 1]
python">nums = [19, 89, 90, 600, 1]
max_nums = 0
for i in nums:if i > max_nums:max_nums = i
print(max_nums)
600

12、用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)

python">scores = [9.8, 9.5, 9.9, 9.3, 8.9, 9.5, 9.6, 9.3, 9.4, 9.6]
python">scores = [9.8, 9.5, 9.9, 9.3, 8.9, 9.5, 9.6, 9.3, 9.4, 9.6]
min_score = 10
min_num = 0
max_score = 0
max_num = 0
for i in range(len(scores)):if scores[i] > max_score:max_score = scores[i]max_num = iif scores[i] < min_score:min_score = scores[i]min_num = i
del scores[min_num]
del scores[max_num]
sum_score = 0
for score in scores:sum_score += score
avg_score = sum_score / len(scores)
print(avg_score)
9.5
python">scores = [9.8, 9.5, 9.9, 9.3, 8.9, 9.5, 9.6, 9.3, 9.4, 9.6]# 对分数列表进行排序
scores.sort()# 去掉一个最高分和一个最低分
scores = scores[1:-1]sum_score = 0
for score in scores:sum_score += score
avg_score = sum_score / len(scores)
print(avg_score)
9.5

13、有两个列表A和B,使用列表C来获取两个列表中公共的元素

例如: A = [1, ‘a’, 4, 90] B = [‘a’, 8, ‘j’, 1] --> C = [1, ‘a’]

python">A = [1, 'a', 4, 90]
B = ['a', 8, 'j', 1]
C = []
for i in A:if i in B:C.append(i)
print(C) 
[1, 'a']

14、获取列表中出现次数最多的元素

python">nums = [1, 2, 3,1,4,2,1,3,7,3,3]
python">nums = [1, 2, 3,1,4,2,1,3,7,3,3]
res = 0
res_i = 0
for i in nums:if nums.count(i) > res:res = nums.count(i)res_i = i
print(res_i)
3

15、将scores中所有的及格的分数提取出来

python">scores = [89, 67, 56, 90, 98, 30, 78, 51, 99]
python">scores = [89, 67, 56, 90, 98, 30, 78, 51, 99]
scores_pass = []
for score in scores:if score >= 60:scores_pass.append(score)
print(scores_pass)
[89, 67, 90, 98, 78, 99]

16、输入一个整数列表,输出其中最大的两个元素的乘积。

python">lst=[2,6,7,10,3,5,4,9,8]
lst.sort(reverse=True)
print(lst[0]*lst[1])
90

17、已知一个数字列表,求所有元素的平均数

python">lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
python">lst = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
sum_score = 0
for score in lst:sum_score += score
avg_score = sum_score / len(lst)
print(avg_score)
125.54545454545455

18、输入一个字符串列表,输出其中最长的字符串。

python">lst = ['apple', 'banana', 'pear', 'orange', 'kiwi', 'mango']
python">lst = ['apple', 'banana', 'pear', 'orange', 'kiwi', 'mango']
res = []
num = 0
for k in lst:if len(k) > num:num = len(k)res = [k]elif len(k) == num:res.append(k)
print(res)
['banana', 'orange']

19、输入一个字符串列表,输出其中第一个字母是元音字母的单词(不区分大小写)。

python">lst = ['apple', 'Banana', 'pear', 'Orange', 'kiwi', 'mango']
python">lst = ['apple', 'Banana', 'pear', 'Orange', 'kiwi', 'mango']
vowels = ['a','e','i','o','u','A','E','I','O','U']
for fruit in lst:for char in vowels:if fruit.startswith(char):print(fruit)
apple
Orange

20、已有两个列表,输出它们的笛卡尔积。

python">lst1 = [1, 2, 3]
lst2 = ['a', 'b','c']
python">lst1 = [1, 2, 3]
lst2 = ['a', 'b','c']
for i in lst1:for j in lst2:print(i, j)
1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c

http://www.ppmy.cn/devtools/57214.html

相关文章

昇思25天学习打卡营第13天 | SSD目标检测

模型简介 SSD&#xff0c;全称Single Shot MultiBox Detector&#xff0c;是Wei Liu在ECCV 2016上提出的一种目标检测算法。使用Nvidia Titan X在VOC 2007测试集上&#xff0c;SSD对于输入尺寸300x300的网络&#xff0c;达到74.3%mAP(mean Average Precision)以及59FPS&#x…

Node.js 入门

目录 定义 什么是前端工程化&#xff1f; Node.js 为何能执行 JS&#xff1f; Node.js 安装 使用 Node.js fs 模块 - 读写文件 path 模块 - 路径处理 案例 - 压缩前端 html URL 中的端口号 常见的服务程序 http 模块-创建 Web 服务 浏览时钟&#xff08;案例&#x…

001 进程和线程

文章目录 进程和线程进程线程 进程和线程 进程–>正在运行的程序&#xff0c;可能对应一个或多个进程&#xff0c; 拥有一些资源&#xff1a;内存、CPU 进程之间可以通信&#xff1a;管道、信号量、共享存储、socket 消息队列等 线程–>执行指令的集合&#xff0c;一个进…

esp12实现的网络时钟校准

网络时间的获取是通过向第三方服务器发送GET请求获取并解析出来的。 在本篇博客中&#xff0c;网络时间的获取是一种自动的行为&#xff0c;当系统成功连接WiFi获取到网络天气后&#xff0c;系统将自动获取并解析得到时间和日期&#xff0c;为了减少误差每两分钟左右进行一次校…

盘点几款国产AI高效神器!打工人赶紧码住

在这个AI技术飞速发展的时代&#xff0c;国产AI工具正成为提升工作效率的得力助手。作为AI工具测评博主&#xff0c;米兔有幸体验了多款国产AI工具&#xff0c;今天要向大家介绍几款超级好用的AI工具。这些工具不仅功能强大&#xff0c;而且操作简便&#xff0c;是职场人士不可…

51单片机第18步_将TIM0用作13位定时器

本章重点学习将TIM0用作13位定时器。 1、定时器0工作在模式0框图 2、定时器0工作在模式0举例 1、Keil C51中有一些关键字&#xff0c;需要牢记&#xff1a; interrupt 0&#xff1a;指定当前函数为外部中断0&#xff1b; interrupt 1&#xff1a;指定当前函数为定时器0中断…

隐私集合求交(PSI)原理深入浅出

隐私集合求交技术是多方安全计算领域的一个子问题&#xff0c;通常也被称为安全求交、隐私保护集合交集或者隐私交集技术等&#xff0c;其目的是允许持有各自数据集的双方或者多方&#xff0c;执行两方或者多方集合的交集计算&#xff0c;当PSI执行完成&#xff0c;一方或者两方…

LUA 语言中subtree 的使用教程

在线编辑器&#xff1a; https://www.runoob.com/try/runcode.php?filenameHelloWorld&typelua 在Lua语言中&#xff0c;"subtree"通常指的是一个子表或者子树&#xff0c;它指的是一个Lua表&#xff08;table&#xff09;中的一个部分&#xff0c;可以是一个单…