""" Auther:少校 Time:2025/3/29 16:24 越努力,越幸运 """ from itertools import count # 1. 生成20个随机整数(范围1-100)的列表 # from random import randint # list1 = [] # for x in range(20): # list1.append(randint(1, 100)) # print(list1) # list1 = [randint(1,100) for x in range(20)] # print(list1) # # 2.将字符串"Python"转为字符列表 # str1="Python" # list1 = list(str1) # print(list1) # # 3.用列表推导式创建1-100的平方数列表 # list1= [x**2 for x in range(1,101)] # print(list1) # # 4.将两个等长列表合并为元组列表 # # 例如: # # 列表1:[10, 20, 30, 40] # # 列表2:[100, 200, 300, 400] # # 结果: [(10, 100), (20, 200), (30, 300), (40, 400)] # list1=[10, 20, 30, 40] # list2=[100, 200, 300, 400] # jieGuo=[] # for x in range(len(list1)): # jieGuo.append((list1[x],list2[x])) # print(jieGuo) # print([(list1[x],list2[x]) for x in range(len(list1))]) # # 5.在列表第3个位置插入新元素 # list1=[10, 20, 30, 40] # list1.insert(2,99) # print(list1) # 6.合并多个列表并去重 # list1=[10, 20, 30, 40] # list2=[100, 200, 300, 400] # list3=list1+list2 # list4=[] # for x in list3: # if x not in list4: # list4.append(x) # print(list4) # 7.交换列表的首尾两个元素 # list1=[10, 20, 30, 40] # # list2=list1.copy() # # list2[-1] = list1[0] # # list2[0] =list1[-1] # # print(list2) # list1[0],list1[-1] = list1[-1],list1[0] # print(list1) # 8.将列表元素转换为绝对值 # abs(数字) 返回数字的绝对值 # list1=[10, -20, -30, 40] # list2=[x if x>0 else x*-1 for x in list1] # print(list2) # print([abs(x) for x in list1]) # 9.找出两个列表的不同元素 # list1=[10, 20, 30, 40] # list2=[100, 200, 30, 400] # list3=list1+list2 # list4=[] # for x in list3: # if (x in list1 and x not in list2) or (x in list2 and x not in list1) : # list4.append(x) # print(list4) # --------------------进阶------------------------- # 1.生成前15个斐波那契数列的列表 # a = 0 # b = 1 # n = 15 # list1=[0,1] # for x in range(n-2): # c= a + b # a= b # b= c # list1.append(b) # print(list1) # n=15 # list1 =[0,1] # for x in range(n-2): # list1.append(list1[-1]+list1[-2]) # print(list1) # 2.删除列表中所有负数 # list1=[10, -20, -30, 40] # list2=[x for x in list1 if x>=0] # print(list2) # 3.找出列表中出现次数最多的元素 # list1 = [10, -20, -30, 40 , 40] # list2 = [] # list3 = [] # for x in list1: # list2.append(list1.count(x)) # max1 = max(list2) # for x in list1: # if list1.count(x) == max1 and x not in list3: # list3.append(x) # print(list3) #print(list1[list2.index(max(list2))]) # 4.统计列表中各数据类型的数量 int1 =0 str1 =0 float1 =0 bool1 =0 list1 = [10, -20, -30, 40 , 40 ,"app",True, 3.14,3.15,9.26,"ddd"] type1= [] count1=[] for x in list1: a = type(x) if a not in type1: type1.append(a) count1.append(1) else: count1[type1.index(a)] +=1 print(type1,count1) # if a == int : # int1 += 1 # elif a == str : # str1 += 1 # elif a == float : # float1 +=1 # elif a == bool : # bool1 += 1 # print(f"int:{int1},str:{str1},float:{float1},bool:{bool1}") # 5.已经一个按照从大到小排好序的列表,提示用户输入一个数字,将输入的数据插入到原列表中,插入后列表仍然按照从小到大排序 # list1=[1,2,3,4,5,6,7,8,9,10] # shuRu=int(input("请输入一个数字:")) # # list1.append(shuRu) # # list1.sort(reverse=True) # # print(list1) # if shuRu > list1[-1]: # list1.append(shuRu) # else: # for x in range(len(list1)): # if shuRu < list1[x]: # list1.insert(x,shuRu) # break # print(list1) # 6.已知两个已经按照从小到大排好序的列表,合并这两个列表,要求合并后的新的列表中的元素仍然从小到大排序 list1=[0,2,4,6,8,10] list2=[1,3,4,5,7,8,9] # list3=list1+list2 # list3.sort() # print(list3) list3=[] while True: if list1 > list2 : list3.append(list2.pop(0)) else: list3.append(list1.pop(0)) if list1 == [] or list2 == []: break list3.extend(list1+list2) print(list3)
01.第一周结尾homework
本节3492字2025-03-31 11:29:28