已知一个数字列表,求列表中心元素。
nums = [1, 3, 4, 5, 34, 634, 32, 5, 1,0]
if len(nums)%2!=0:
print(nums[(len(nums)//2)])
else:
print(nums[(len(nums)//2-1):(len(nums)//2+1)])
已知一个数字数列,求所有元素和:
num1 = 0 #元素和
nums = [1, 3, 4, 5, 34, 634, 32, 5, 1,0]
for yuansu in nums:
num1 += yuansu
print(num1)
print('==============================================')
已知一个数字数列,输出所有奇数下标元素。
nums = [1, 3, 4, 5, 34, 634, 32, 5, 1,0]
for index in range(len(nums)):
if (nums[index]) % 2 != 0:
print(index,end='')
print()
已知一个数列,将列表所有元素乘以2
num3=[]
nums = [1, 3, 4, 5, 34, 634, 32, 5, 1,0]
for num2 in nums:
# print(str(num2*2))
num3.append(str(num2*2))
print(num3)
7.已经一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表
# 例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
list1 = [97, 98, 99] #list1 = ['a', 'b', 'c']
for x2 in list1:
print(chr(x2))
8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
nums = [66, 90, 45, 88, 55, 87]
high_num = 0
small_num = 1000
for num4 in nums:
if high_num < num4:
high_num = num4
print('最大的数是:%d' % high_num)
for num4 in nums:
if small_num > num4:
small_num = num4
print('最小的数是:%d' % small_num)
print((sum(nums)- small_num - high_num))
print('========================================')
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 x in A:
for y in B:
if x==y:
z=(str(x))
c.append(z)
print(c)