day7-作业

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)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • """author = Zard"" 1.已知一个数字列表,求列表中心元素。 2.已知一个数字列表,求所有元素和。...
    Zard泉水阅读 87评论 0赞 0
  • 1.已知一个数字列表,求列表中心元素。 print('=============第一题===========') ...
    小逗比的奋斗史阅读 190评论 0赞 0
  • 1.已知一个数字列表,求列表中心元素。 2.已知一个数字列表,求所有元素和。 3.已知一个数字列表,输出所有奇数下...
    是喵啊阅读 305评论 0赞 0
  • 除去营养状况外,孩子的身高有70%的主动权掌握在父母手里,父母的遗传是决定孩子身高的主要因素,因为决定身高的因素3...
    d0aaef9682e2阅读 851评论 0赞 0
  • 我上小学时极爱背诗,我觉得诗里有古韵,学期初语文课本一分发下来我就直奔古诗而去,总要背个烂熟于胸的,因为迟早要求背...
    陈厂长阅读 475评论 6赞 6

友情链接更多精彩内容