1.已知一个数字列表,求列表中心元素。
print("=============第1题=============")
nums = [1,2,3,4,5,6]
lenth = len(nums)
if lenth %2 == 0:
print(nums[int(lenth/2-1)],nums[int(lenth/2)])
else:
print(nums[lenth//2])
2.已知一个数字列表,求所有元素和。
print("=============第2题=============")
nums = [1,2,3,4,5,6]
print(sum(nums))
3.已知一个数字列表,输出所有奇数下标元素。
print("=============第3题=============")
nums = [1,2,3,4,5,6]
lenth = len(nums)
for index in range(lenth):
if index % 2 == 1:
print(nums[index],end=",")
print()
4.已知一个数字列表,输出所有元素中,值为奇数的元素。
print("=============第4题=============")
nums = [1,2,3,4,5,6]
for num in nums:
if num % 2 == 1:
print(num,end=",")
print()
5.已知一个数字列表,将所有元素乘二。
print("=============第5题=============")
nums = [1,2,3,4,5,6]
new_nums = []
for num in nums:
num *= 2
new_nums.append(num)
print(new_nums)
6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']
print("=============第6题=============")
name_list = ["陈泓羽","张三","李四","大黄","张三","李四","王二","麻子","李四","陈泓羽"]
newname_list = []
for name in name_list:
if name not in newname_list:
newname_list.append(name)
print(newname_list)
7.已知一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
print("=============第7题=============")
list1=[1,2,3,55,68,100,121,231]
new_list = []
for item in list1:
new_list.append(chr(item))
print(new_list)
8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
print("=============第8题=============")
list1 = [98,80,56,78,23,95,99,90,92]
max1 = max(list1)
min1 = min(list1)
list1.remove(max1)
list1.remove(min1)
sum1 = sum(list1)
average = sum1/len(list1)
print(average)
9.有两个列表A和B,使用列表C来获取两个列表中公共的元素
例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']
print("=============第9题=============")
A = [1, 'a', 4, 90]
B = ['a', 8, 'j', 1]
C = []
for item in A:
if item in B:
C.append(item)
print(C)
10.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)
例如: nums = [19, 89, 90, 600, 1] —> 600
print("=============第10题=============")
nums = [19, 89, 90, 600, 1]
max1 = nums[0]
for num in nums:
if num > max1:
max1 = num
print(max1)
11.获取列表中出现次数最多的元素
例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3
print("=============第11题=============")
nums = [1, 2, 3,1,4,2,1,3,7,3,3]
new_num = []
max1 = nums.count(nums[0])
for num in nums:
if nums.count(num) > max1:
max1 = nums.count(num)
for num in nums:
if nums.count(num) == max1 and num not in new_num:
new_num.append(num)
print(new_num)