美文网首页
Day7-作业

Day7-作业

作者: SheeranED | 来源:发表于2019-06-12 17:46 被阅读0次

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

list1 = [1, 3, 5, 5, 5, 7]
l1 = len(list1)
if int(l1 % 2) == 0:
    print(list1[int(l1/2)-1:int(l1/2)+1])
else:
    print(list1[int(l1/2):int(l1/2)+1])

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

list1 = [1, 3, 5, 5, 5, 7]
print(sum(list1))

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

list1 = [1, 3, 5, 5, 5, 7]
for index in range(1, len(list1), 2):
    print(index, list1[index])

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

list2 = [1, 3, 6, 4, 5, 5, 5, 7]
new1 = []
for item in list2:
    if item % 2 != 0:
        new1.append(item)
print(new1)

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

list2 = [1, 3, 6, 4, 5, 5, 5, 7]
new1 = []
for item in list2:
    new_item = item * 2
    new1.append(new_item)
print(new1)
nums = [1, 2, 3, 4]
for index in range(len(nums)):
    nums[idenx] *= 2
print(nums)

6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的

例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']

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']

list1 = [56, 98, 97, 54, 45]
list2 = []
for item in list1:
    list2 += [chr(item)]
print(list2)

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

score1 = [99, 98, 88, 78, 86, 85, 78]
score1.remove(max(score1))
score1.remove(min(score1))
print(sum(score1)/len(score1))

9.有两个列表A和B,使用列表C来获取两个列表中公共的元素

list1 = [1, 2, 3, 4, 5, 6, 7]
list2 = [1, 3, 5, 7, 9, 8]
list3 = []
for item in list1:
    for x in list2:
        if item == x:
            list3.append(item)
print(list3)

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

list1 = [1, 2, 3, 4, 5, 6, 7, 10]
max1 = list1[0]
for item in list1[1:]:
    if item > max1:
        max1 = item
print(max1)

11.获取列表中出现次数最多的元素

list1 = [1, 2, 3, 4, 5, 6, 3, 3, 4, 4, 10]
max1 = 0
for item in list1:
    cs = list1.count(item)
    if cs > max1:
        max1 = cs
max_list = []
for item in list1:
    if max1 == list1.count(item):
        if item not in max_list:
            max_list.append(item)
print(max_list)

相关文章

  • day7-作业

    1、实现点击按钮,滚动条走动和百分比走动 结果 2、实现秒表 结果 3.文字时钟 4、处理classname兼容 ...

  • Day7-作业

    滚动条 秒表 文字时间 微信倒计时 classname兼容

  • Day7-作业

    编写⼀个函数,求1+2+3+...+N 结果: 请输入n值:101-n的和为55 编写⼀个函数,求多个数中的最⼤值...

  • day7-作业

    1.编写一个函数,求1+2+3+...+N 2.编写一个函数,求多个数中的最大值 3.编写一个函数,实现摇色子的功...

  • day7-作业

    1.编写一个函数,求1+2+3+...+N 结果 2.编写一个函数,求多个数中的最大值 结果 3.编写一一个函数,...

  • DAY7-作业

    题目一,编写一个函数,求1+2+3+...+n的和 代码实现: 输出结果: 题目二, 代码实现; 输出结果: 题目...

  • day7-作业

    import copyname_students_information = {}number_students_...

  • Day7-作业

  • day7-作业

  • Day7-作业

    1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) 2.声...

网友评论

      本文标题:Day7-作业

      本文链接:https://www.haomeiwen.com/subject/xvcrfctx.html