python的题目?

6. 列表与元组实验(3学时,8分)
(1) 编程:将列表的元素按逆序重新存放。
(2) 编程:将列表中的偶数变成其平方值,奇数保持不变。
(3) 编程:生成包含100个100以内的随机正整数的元组,统计每个数出现的次数。
(4) 编程:输入5 X 5 的矩阵a,完成下列要求:
a. 输出矩阵a
b. 将第2行和第5行元素对调后,再重新输出a
7. 字典与集合实验(3学时,6分)
(1) 编程:创建由星期一到星期日的7个值组成的字典,输出键列表、值列表和键值列表。
(2) 编程:输入10名学生的姓名和成绩,输出其最高分和最低分。要求使用字典存放学生的姓名和成绩。
(3) 编程:随机产生10个[0,10]范围的整数,分别组成集合A和集合B。输出集合A、集合B的内容、长度以及他们的并集、交集和差集。

1.将列表的元素按逆序重新存放。

my_list = [1, 2, 3, 4, 5]
my_list.reverse() # 将列表元素反转
print(my_list) # 输出反转后的列表

2.将列表中的偶数变成其平方值,奇数保持不变。

my_list = [1, 2, 3, 4, 5]
for i in range(len(my_list)):
if my_list[i] % 2 == 0: # 如果元素是偶数
my_list[i] = my_list[i] ** 2 # 将元素平方
print(my_list) # 输出更改后的列表

3.生成包含100个100以内的随机正整数的元组,统计每个数出现的次数。

生成包含100个100以内的随机正整数的元组,统计每个数出现的次数。

4.输入5X5的矩阵a,完成下列要求:a.输出矩阵ab.将第2行和第5行元素对调后,再重新输出a

# a.
a = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]]

for row in a:
print(row) # 每行输出矩阵

# b.
a[1], a[4] = a[4], a[1] # 将第2行和第5行元素对调

# c.
for row in a:
print(row) # 重新输出矩阵

5.创建由星期一到星期日的7个值组成的字典,输出键列表、值列表和键值列表。

    days_of_week = {
    "Monday": 1,
    "Tuesday": 2,
    "Wednesday": 3,
    "Thursday": 4,
    "Friday": 5,
    "Saturday": 6,
    "Sunday": 7
    }

    keys = list(days_of_week.keys()) # 将字典键转换为列表
    values = list(days_of_week.values()) # 将字典值转换为列表
    key_values = list(days_of_week.items()) # 将字典键值对转换为列表

    print("Keys:", keys) # 输出键列表
    print("Values:", values) # 输出值列表
    print("Key-Values:", key_values) # 输出键值列表

    6.输入10名学生的姓名和成绩,输出其最高分和最低分。要求使用字典存放学生的姓名和成绩。

    my_dict = {}
    for i in range(10):
    name = input("Enter student name: ") # 输入学生姓名
    score = int(input("Enter student score: ")) # 输入学生成绩
    my_dict[name] = score # 将学生姓名和成绩存入字典

    highest_score = max(my_dict.values()) # 获取最高成绩
    lowest_score = min(my_dict.values()) # 获取最低成绩

    print("Highest score: ", highest_score) # 输出最高成绩
    print("Lowest score: ", lowest_score) # 输出最低成绩

    7.随机产生10个[0,10]范围的整数,分别组成集合A和集合B。输出集合A、集合B的内容、长度以及他们的并集、交集和差集。

    import random

    A = set(random.sample(range(11), 10)) # 随机从0到10范围内选取10个不重复的整数,将其作为集合A的元素
    B = set(random.sample(range(11), 10)) # 随机从0到10范围内选取10个不重复的整数,将其作为集合B的元素

    print("Set A: ", A) # 输出集合A
    print("Set B: ", B) # 输出集合B

    print("Length of A: ", len(A)) # 输出集合A的长度
    print("Length of B: ", len(B)) # 输出集合B的长度

    print("Union of A and B: ", A.union(B)) # 输出集合A和集合B的并集
    print("Intersection of A and B: ", A.intersection(B)) # 输出集合A和集合B的交集
    print("Difference of A and B: ", A.difference(B)) # 输出集合A和集合B的差集

    温馨提示:答案为网友推荐,仅供参考
    第1个回答  2023-02-08
    6.(1) 编程:将列表的元素按逆序重新存放。
    def reverseList(list):
    list.reverse()
    return list

    (2) 编程:将列表中的偶数变成其平方值,奇数保持不变。
    def squareEvenNumber(list):
    for i in range(len(list)):
    if list[i] % 2 == 0:
    list[i] = list[i] * list[i]
    return list

    (3) 编程:生成包含100个100以内的随机正整数的元组,统计每个数出现的次数。
    import random
    def countRandom():
    nums = tuple(random.randint(0,100) for i in range(100))
    count = {}
    for i in nums:
    if i not in count:
    count[i] = 1
    else:
    count[i] += 1
    return count

    (4) 编程:输入5 X 5 的矩阵a,完成下列要求:
    a. 输出矩阵a
    b. 将第2行和第5行元素对调后,再重新输出a

    def matrixChange(matrix):
    row_2 = matrix[1]
    row_5 = matrix[4]
    matrix[1] = row_5
    matrix[4] = row_2
    return matrix

    matrix = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]

    print('输出矩阵a:')
    for row in matrix:
    for col in row:
    print(col, end=' ')
    print()

    matrix = matrixChange(matrix)
    print('将第2行和第5行元素对调后,再重新输出a:')
    for row in matrix:
    for col in row:
    print(col, end=' ')
    print()
    7.(1)
    week_dict = {"Monday": 1, "Tuesday": 2, "Wednesday": 3, "Thursday": 4, "Friday": 5, "Saturday": 6, "Sunday": 7}
    keys = list(week_dict.keys())
    values = list(week_dict.values())
    key_values = list(week_dict.items())
    print(keys)
    print(values)
    print(key_values)

    (2)
    student_dict = {}
    total = 0
    lowest = 11
    highest = 0
    for i in range(10):
    name = input("Please enter the student's name: ")
    score = int(input("Please enter the student's score: "))
    student_dict[name] = score
    total += score
    if score < lowest:
    lowest = score
    if score > highest:
    highest = score
    print("The highest score is", highest)
    print("The lowest score is", lowest)

    (3)
    import random
    A = set()
    B = set()
    for i in range(10):
    A.add(random.randint(0, 10))
    B.add(random.randint(0, 10))
    print("The set A is", A)
    print("The set B is", B)
    print("The length of A is", len(A))
    print("The length of B is", len(B))
    print("The union of A and B is", A | B)
    print("The intersection of A and B is", A & B)
    print("The difference of A and B is", A - B)
    相似回答