Python练习之列表

1、输入一个包含若干整数的列表,输出新列表,要求新列表中的所有元素来自于输入的列表,并且降序排列。

python 复制代码
a=input("输入列表元素:")
item=a.split(" ")
list=[eval(x) for x in item]
list.sort(key=None,reverse=True)
print(list)

输入列表元素:1 2 3 7 5
[7, 5, 3, 2, 1]

2、列表生成式。使用列表生成式生成列表,其元素为 100 以内所有能被 3 整除的数。

python 复制代码
list=[x for x in range(100) if x%3==0]
print(list)

[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

3、给出一个包含若干整数的列表23,16,18,19,76,121,33,57,80,输出一个新列表,要求新列表中只包含原列表中的偶数。提示:可以使用 list、filter、lambda 函数完成。

python 复制代码
this_list=[23,16,18,19,76,121,33,57,80]
new_list=list(filter(lambda x:x%2==0,this_list))
print(new_list)

4、给出一个包含若干整数的列表23,16,18,19,76,121,33,57,80,输出列表中的所有整数连乘的结果。提示:可以使用 reduce、lambda 函数完成。

python 复制代码
from functools import reduce

a=[23,16,18,19,76,121,33,57,80]
result=reduce(lambda x,y:x*y,a)
print(result)

5、列表切片

编写程序,用户输入一个列表和两个整数作为下标,然后用切片获取并输出介入两个下标之间的元素组成的子列表。例如,用户输入1,2,3,4,5,6和 2,5 后,程序输出3,4,5,6

python 复制代码
a=input("输入列表元素:")
item=a.split(" ")
l=[eval(x) for x in item]
print(l)
x=int(input("start:"))
y=int(input("end:"))
print(l[x:y:])


输入列表元素:1 2 3 4 5 6
[1, 2, 3, 4, 5, 6]

start:2

end:5
[3, 4, 5]

6、单词长度(crr23)

输入一句英文句子,例如:the scenery along the should be and the mood at the view,输出其中最长的单词及长度。

python 复制代码
sentence=input("请输入一句英文:")
word=sentence.split() # 把句子分成单词
length=[len(x) for x in word] # 每个单词的长度
lens=max(length)
print("字母{}及其长度{}".format(word[length.index(lens)],lens)) # 最长字母第一次出现的位置,通过下标找到该字母

the scenery along the should be and the mood at the view
['the', 'scenery', 'along', 'the', 'should', 'be', 'and', 'the', 'mood', 'at', 'the', 'view']
[3, 7, 5, 3, 6, 2, 3, 3, 4, 2, 3, 4]
字母scenery及其长度7

7、输入两个分别包含 2 个整数的列表,分别表示城市中两个地点的坐标,输出两个点之间的曼哈顿距离。

曼哈顿距离是指在城市中两个地点的坐标上,通过计算水平和垂直方向上的距离之和得出的距离。对于给定的两个点p1 = (x1, y1) 和 p2 = (x2, y2),曼哈顿距离可以通过计算|x1 - x2| + |y1 - y2|来得到。在编程中,可以输入两个包含2个整数的列表,分别表示两个地点的坐标,然后使用这个公式计算出曼哈顿距离

python 复制代码
point1 = eval(input("请输入point1的坐标列表:"))
point2 = eval(input("请输入point2的坐标列表:"))
distance = map(lambda x, y: abs(x - y), point1, point2)
manhattan_distance = sum(distance)
print("两个点间的曼哈顿距离:", manhattan_distance)


请输入point1的坐标列表:[1,2]

请输入point2的坐标列表:[3,4]
两个点间的曼哈顿距离: 4

8、高考录取率(列表与元组 crr39)

根据十年高考录取率表创建列表,并完成如下操作:

① 计算十年平均录取率。

② 找出录取率最高的年份。

python 复制代码
year=[(2006,57),(2007,56),(2008,57),(2009,62),(2010,67),(2011,72),(2012,75),(2013,76),(2014,74.3),(2015,74)]

rate=[x[1] for x in year]
avg=sum(rate)/len(rate)
print("平均录取率{}".format(avg))
max_avg=max(rate)
max_year=year[rate.index(max_avg)]
print("录取最高年份{}".format(max_year[0]))

9、列表二分查找(crr65)

二分查找是一个经典的算法,用来在有序的一组数中快速找到待查找的数。所谓"二分",

就是每次操作都将查找范围一分为二,即将查找区间缩小一半,直到找到或查询了所有区间

都没有找到要查找的数据为止。利用二分查找法,查找[34, 64, 67, 72, 73, 82, 83, 85, 87, 88, 90,

91, 96, 98]中指定数字的索引号

python 复制代码
l=[34,64,67,72,73,82,83,85,87,88,90,91,96,98]
x=int(input("请输入待查找的数:"))
low=0
high=len(l)-1

while low<=high:
    mid=(low+high)//2
    if l[mid]<x:
        low=mid+1
    elif l[mid]>x:
        high=mid-1
    else:
        print("THE NUMBER {} IS FOUND,AND THE INDEX IS {}".format(x,mid))
        break;
else:
    print("THERE IS NO{} IN THE LIST".format(x))

10、编写程序,输入一个大于 2 的自然数,然后输出小于该数字的所有素数组成的列表。

要求:利用列表、内置函数 enumerate()、filer()来实现。

python 复制代码
maxNumber=int(input("请输入一个大于2的自然数:"))
lst=list(range(2,maxNumber))
#最大整数的乎方板
m=int(maxNumber**0.5)
for index,value in enumerate(lst):
    #如果当前数字已大于最大整数的乎方根,结束判断
    if value > m:
        break
    lst[index+1:]=filter(lambda x:x%value !=0,list[index+1:])
print(lst)
相关推荐
默_笙4 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
rockmelodies4 天前
# Windows Server2008 R2 Standard(SP1镜像U盘)重置本地管理员密码【完整详细步骤】
windows·密码破解
qq_426003964 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
神仙别闹4 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技4 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时4 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊4 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型