1.已知一个数字列表,求列表中心元素。
list1 = [1, 2, 3, 4, 5, 6, 7, 8]
if len(list1) % 2 == 0:
print(list1[int(len(list1)/2-1)], list1[int(len(list1)/2+1)])
else:
print(list1[int(len(list1)/2)])
2.已知一个数字列表,求所有元素和。
nums = [12, 345, 34, 32, 96, 69]
print(sum(nums))
3.已知一个数字列表,输出所有奇数下标元素。
nums1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 21, 32, 73, 94, 71]
for index in range(len(nums1)):
if nums1[index] % 2 != 0:
print(index, end=' ')
print()
4.已知一个数字列表,输出所有元素中,值为奇数的元素。
print('************************************************************')
nums2 = [1, 2, 13, 4, 5, 6, 7, 8, 24, 11, 21, 32, 79, 93, 75]
for index in range(len(nums2)):
if nums2[index] % 2 != 0:
print(nums2[index], end=' ')
print()
print('=====================5-->9=========================')
5.已知一个数字列表,将所有元素乘二。
例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]
nums3 = [1, 2, 3, 4, 5, 6, 7]
new_nums3 = []
for item in nums3:
new_nums3 += [2*item]
print(new_nums3)
6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']
names = ['张三', '李四', '张三', '李四', '张三', '大黄', '王二麻子', '张三', '李四', '李四']
new_names = []
for item in names:
if item not in new_names:
new_names += [item]
print(new_names)
for item in names[:]:
if names.count(item) > 1:
names.remove(item)
print(names)
7.已经一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
list2 = [97, 98, 99, 194, 939, 1993]
new_list2 = []
for item in list2:
new_list2 += [chr(item)]
print(new_list2)
8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
scores = [89, 73, 65, 88, 98, 76, 59, 100]
scores.remove(max(scores))
scores.remove(min(scores))
print(sum(scores)/len(scores))
9.有两个列表A和B,使用列表C来获取两个列表中公共的元素
例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']
list3 = [1, 'a', 4, 90, 8, 'j']
list4 = ['a', 8, 'j', 1, 90]
list5 = []
for item1 in list3:
for item2 in list4:
if item1 == item2:
list5 += [item1]
print(list5)
10.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)
例如: nums = [19, 89, 90, 600, 1] —> 600
nums4 = [19, 89, 90, 600, 1]
max1 = nums4[0]
for index in range(len(nums4)):
if nums4[index] > max1:
max1 = nums4[index]
print(max1)
11.获取列表中出现次数最多的元素
例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3
nums = [1, 2, 3, 1, 4, 2, 1, 3, 7, 3, 3]
max1 = 0
for item in nums:
ct = nums.count(item)
if ct > max1:
max1 = ct
max_num = []
for item in nums:
if max1 == nums.count(item):
if item not in max_num:
max_num.append(item)
print(max_num)