1.已知一個數(shù)字列表靡狞,求列表中心元素。
num_str = input("請輸入一列數(shù)字隔嫡,用空格隔開:")
num_list = num_str.split()
lenth = len(num_list)
if lenth%2 == 0:
print("中心元素為:{}和{}".format(num_list[lenth//2-1],num_list[lenth//2]))
else:
print("中心元素為:{}".format(num_list[(lenth-1)//2]))
2.已知一個數(shù)字列表甸怕,求所有元素和。
num_list = [1, 2, 3, 4, 5]
sum_list = sum(num_list)
print("和:",sum_list)
3.已知一個數(shù)字列表腮恩,輸出所有奇數(shù)下標(biāo)元素梢杭。
num_list = [1, 2, 3, 4, 5, 6]
index = 1
while index <len(num_list):
print(num_list[index], end = ' ')
index += 2
4.已知一個數(shù)字列表,輸出所有元素中秸滴,值為奇數(shù)的元素武契。
num_list = [1, 2, 3, 4, 5, 6, 7, 8]
for item in num_list:
if item%2 != 0:
print(item, end = ' ')
5.已知一個數(shù)字列表,將所有元素乘二缸榛。
例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]
nums = [1, 2, 3, 4]
for index in range(len(nums)):
nums[index] *= 2
print(nums)
6.有一個長度是10的列表吝羞,數(shù)組內(nèi)有10個人名兰伤,要求去掉重復(fù)的
例如:names = ['張三', '李四', '大黃', '張三'] -> names = ['張三', '李四', '大黃']
names = ['張三', '李四', '大黃', '張三']
names.reverse()
for item in names[:]:
if names.count(item) > 1:
names.remove(item)
names.reverse()
print(names)
7.已知一個數(shù)字列表(數(shù)字大小在0~6535之間), 將列表轉(zhuǎn)換成數(shù)字對應(yīng)的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
list1 = [97, 98, 99]
for index in range(len(list1)):
list1[index] = chr(list1[index])
print(list1)
8.用一個列表來保存一個節(jié)目的所有分數(shù)内颗,求平均分數(shù)(去掉一個最高分,去掉一個最低分敦腔,求最后得分)
scores = [1, 2, 3, 4, 5, 6, 7, 8, 9]
scores.remove(max(scores))
scores.remove(min(scores))
sum = 0
for item in scores:
sum += item
print("平均分為:", sum/len(scores))
9.有兩個列表A和B均澳,使用列表C來獲取兩個列表中公共的元素
例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']
A = [1, 'a', 4, 90]
B = ['a', 8, 'j', 1]
C = []
for item_A in A:
if item_A in item_B and item_A not in C:
C.append(item_A)
print(C)
10.有一個數(shù)字列表,獲取這個列表中的最大值.(注意: 不能使用max函數(shù))
例如: nums = [19, 89, 90, 600, 1] —> 600
nums = [19, 89, 90, 600, 1]
nums.sort()
print(nums[-1])
11.獲取列表中出現(xiàn)次數(shù)最多的元素
例如: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]
max_count = 0
for item in nums:
if nums.count(item) > max_count:
max_count = nums.count(item)
new_nums = []
for item in nums:
if nums.count(item) == max_count:
if item not in new_nums:
new_nums.append(item)
for item in new_nums:
print(item , end = ' ')