"""author = 曹琦"""
1.已知一个数字列表,求列表中心元素。
list1 = [1,2,3,4,5,6]
num1 = 0
for char in list1:
num1 += 1
if num1 % 2 == 0:
num2 = num1//2-1
num3 = num2 + 2
print(list1[num2:num3])
else:
num2 = num1 // 2
print(list1[num2])
2.已知一个数字列表,求所有元素和。
list1 = [1,2,3,4,5,6]
sum1 = 0
for char in list1:
sum1 += char
print(sum1)
3.已知一个数字列表,输出所有奇数下标元素。
list1 = ['a','b','123','sde','c','ddd']
num1 = 0
for char in list1:
num1 += 1
print(list1[1::2])
4.已知一个数字列表,输出所有元素中,值为奇数的。
list1 = [1,2,3,4,5,6,7,8,9,10]
for char in list1:
if char % 2 !=0:
print(char,end=',')
5.已知一个数字列表,将所有元素乘二。
list1 = [1,2,3,4,5,6,7,8,9,10]
for index in range(len(list1)):
list1[index] *= 2
print(list1)
6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']
names = ['张三', '李四', '大黄', '张三']
new_names = []
for char in names:
if not char in new_names:
new_names.append(char)
print(new_names)
7.已经一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
list1 = [97, 98, 99]
for index in range(len(list1)):
list1[index] == 'ord(list1[index])'
print(list1)
8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
list1 = [97,90,99,23,45,76,23,46,]
num1 = 0
max1 = list1[0]
min1 = list1[0]
sum1 = 0
for char in list1:
num1 += 1
sum1 += char
if char > max1:
max1 = char
if char < min1:
min1 = char
print('最高分为:',max1)
print('最低分为:',min1)
average_score = (sum1 - max1 - min1 )/num1
print('平均分数为:',average_score)
9.有另个列表A和B,使用列表C来获取两个列表中公共的元素
例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']
A = [1, 'a', 4, 90,'a']
B = ['a', 8, 'j', 1]
C = []
for char1 in A:
for char2 in B:
if char1 == char2:
if not char1 in C:
C.append(char1)
print(C)