2019-07-26 day5 作业1

1.已知一个数字列表,求列表中心元素。

#当数字列表为偶数个时,输出中间两个,奇数输出中间一个
num = [1, 3, 4,5] #num列表可以是任意的数字列表
print(num[len(num)//2], end=' ')
if not len(num) & 1 :
    print(num[len(num)//2-1])

2.已知一个数字列表,求所有元素和。

sum1 = 0
for i in num:
  sum1 += i
else:
  print(sum1)

3.已知一个数字列表,输出所有奇数下标元素。

for index in range(1,len(num),2):
  print(num[index], end= ',')

4.已知一个数字列表,输出所有元素中,值为奇数的元素。

for num_index in num[:]:
  if num_index & 1:
    print(num_index)

5.已知一个数字列表,将所有元素乘二。

for index in range(0, len(num)):
  num[index] *= 2
else:
    print(num)

6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']

names = ['张三', '李四', '大黄', '张三','王五', '李四', '大黄', '顺溜','张三', '李四']
for index in range(0,10):    #长度10可根据列表的长度来确定
    for name in names[index+1:]:
        if names[index] == name:
            names.remove(name)
else:
    print(names)

7.已经一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']

for index in range(0, len(list1)):
    list1[index] = chr(list1[index])
else:
    print(list1)

8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)

list_grades = [91, 19, 40, 80, 20, 30]
total = 0
for list_grade in list_grades[:]:
    total += list_grade
else:
    print((total-min(list_grades)-max(list_grades))/(len(list_grades)-2))

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 a in A[:]:
    for b in B[:]:
        if a == b:
            C.append(a)
else:
    print(C)

10.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)

例如: nums = [19, 89, 90, 600, 1] —> 600

nums = [19, 89, 90, 600, 1]
for num in nums[:]:
    if num > max_num:
        nums[0]= num
else:
    print(nums[0])

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] 
most_num = nums[0] #出现最多次数的数
most_count1 = 1 #出现最多的次数
most_count2 = 1 #打擂者的次数
for index in range(len(nums)):
    for num in nums[index+1:]:
        if nums[index] == num:
            most_count2 += 1
            if most_count2 >most_count1:
                most_count1 = most_count2
                most_num = num
    else:
        most_count2 = 1
else:
    print(most_num)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • day7-作业 1. 已知一个数字列表,求列表中心元素。 nums = [2, 5, 12, 45, 55, 12...
    Octane阅读 376评论 0 1
  • 1.已知一个列表,求列表中心元素。 list1 = [4,5,76,55,14,67,43]number = le...
    烧了回忆取暖丶阅读 475评论 0 1
  • 1.已知一个数字列表,求列表中心元素。 list1=[2,3,4,45,6,6,7,7]if len(list1)...
    pythonefb6阅读 793评论 0 0
  • work1 1.已知一个数字列表,求列表中心元素。 2.已知一个数字列表,求所有元素和。 3.已知一个数字列表,输...
    Lis_reak阅读 346评论 0 0
  • 1.已知一个数字列表,求列表中心元素。 2.已知一个数字列表,求所有元素和。 3.已知一个数字列表,输出所有奇数下...
    风中逐月fzzy阅读 179评论 0 0

友情链接更多精彩内容