• Python——集合与字典练习


    集合与字典练习

    question1

    问题描述:有一个列表,其中包括 10 个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表的最后,然后输出这个列表。最终样式是[2,3,4,5,6,7,8,9,0,1]
    代码如下:

    list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    print(list)
    a = list.pop(0)
    list.append(a)
    print(list)
    

      

    结果如下图:

    question2

    问题描述:按照下面的要求实现对列表的操作:
    1). 产生一个列表,其中有 40 个元素,每个元素是 50 到 100 的一个随机整数
    2). 如果这个列表中的数据代表着某个班级 40 人的分数,请计算成绩低于平均分的学生人数
    3). 对上面的列表元素从大到小排序并输出li.sort(reverse=True)

    import random
    
    # 定义一个空列表
    list = []
    
    for i in range(40):
        list.append(random.randint(50, 100))
    
    # 计算平均分
    allScore = 0
    for score in list:
        allScore = allScore + int(score)
    
    averScore = allScore/40
    print(averScore)
    
    flag = 0
    for score in list:
        if score < averScore:
            flag += 1
    
    print(flag)
    
    list.sort(reverse=True)
    print(list)
    

      

    结果如下图:

    question3

    问题描述:
    如果将一句话作为一个字符串,那么这个字符串中必然会有空格(这里仅讨论英文),比如"How are you.",但有的时候,会在两个单词之间多大一个空格。现在的任务是,如果一个字符串中有连续的两个空格,请把它删除。

    s = 'How    are   you '
    word = s.split()
    
    new_s = " ".join(word)
    print(new_s)
    

      

    结果:


    原文:https://blog.csdn.net/weixin_41460135/article/details/90449349

  • 相关阅读:
    log4net详细配置说明
    step by step 之餐饮管理系统三
    CF div2 331 C
    CF div2 331 B
    CF div2 331 A
    poj 2065 还是gauss消元
    poj 3478 poj 3090(欧拉函数的应用)
    poj 1284 求原根的个数(欧拉函数的应用)
    poj 2991 线段树
    poj 1753 poj3185
  • 原文地址:https://www.cnblogs.com/qbdj/p/10938669.html
Copyright © 2020-2023  润新知