Job -8/30

Job -8/30

  1. Has the following set of values ​​[11,22,33,44,55,66,77,88,99,90 ...], all values ​​greater than 66 will be saved to the first key in the dictionary, the value will be less than 66 to a value stored in the second key

    '''
    有如下值集合 [11,22,33,44,55,66,77,88,99,90...]
    将所有大于 66 的值保存至字典的第一个key中
    将小于 66 的值保存至第二个key的值中
    
    即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
    
    '''
    lt = [11, 22, 33, 44, 55, 66, 77, 88, 99]
    lt1 = []
    lt2 = []
    for i in lt:
        if i > 66:
            lt1.append(i)
        elif i == 66:
            continue
        else :
            lt2.append(i)
    print({'k1': lt1, 'k2': lt2})
    

    The result:

    {'k1': [77, 88, 99], 'k2': [11, 22, 33, 44, 55]}
    
    Process finished with exit code 0
  2. Statistical results for each word number s = 'hello alex alex say hello sb sb' are as: { 'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}

    '''
    统计s='hello alex alex say hello sb sb'中每个单词的个数
    
    结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
    
    '''
    s='hello alex alex say hello sb sb'
    
    dic = {}
    
    #print(s.split(' '))
    
    for i in s.split(' '):
        dic.setdefault(i,s.count(i))
    
        # print(dic)
    
    print(dic)

    The result:

    {'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
    
    Process finished with exit code 0
  3. Write code, the following variables, please realize each function in accordance with the requirementsname = " aleX"

    1) corresponding to the variable name is removed on both sides of the space values, and outputs the processing result

    2) determination whether the value corresponding to the variable name beginning with "al", and outputs the result

    3) corresponding to the variable name is determined whether the value of an "X" at the end, and outputs the result

    4) the value corresponding to the variable name in the "l" is replaced with "p", and outputs the result

    5) The name of the corresponding variable value in accordance with "l" division, and outputs the result

    6) For output of the second character corresponding to the variable name values

    7) For the first three characters output value corresponding to the variable name

    After 8) Make an output value corresponding to the variable name in two characters

    9) Make an output value corresponding to the variable name index where "e" position

    10) obtaining sequence, remove the last character. Such as: oldboythe acquisitionoldbo

    '''
    3. 写代码,有如下变量,请按照要求实现每个功能
    name = " aleX"
    # 1)    移除 name 变量对应的值两边的空格,并输出处理结果
    # 2)    判断 name 变量对应的值是否以 "al" 开头,并输出结果
    # 3)    判断 name 变量对应的值是否以 "X" 结尾,并输出结果
    # 4)    将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果
    # 5)    将 name 变量对应的值根据 “l” 分割,并输出结果。
    # 6)    请输出 name 变量对应的值的第 2 个字符?
    # 7)    请输出 name 变量对应的值的前 3 个字符?
    # 8)    请输出 name 变量对应的值的后 2 个字符?
    # 9)    请输出 name 变量对应的值中 “e” 所在索引位置?
    # 10)    获取子序列,去掉最后一个字符。如: oldboy 则获取 oldbo。
    '''
    name = " aleX"
    # 1)    移除 name 变量对应的值两边的空格,并输出处理结果
    print('1:',name.strip())
    # 2)    判断 name 变量对应的值是否以 "al" 开头,并输出结果
    print('2:',name.startswith('al'))
    # 3)    判断 name 变量对应的值是否以 "X" 结尾,并输出结果
    print('3:',name.endswith('X'))
    # 4)    将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果
    print('4:',name.replace('l', 'p'))
    # 5)    将 name 变量对应的值根据 “l” 分割,并输出结果。
    print('5:',name.split('l'))
    # 6)    请输出 name 变量对应的值的第 2 个字符?
    print('6:',name[1])
    # 7)    请输出 name 变量对应的值的前 3 个字符?
    print('7:',name[0:3])
    # 8)    请输出 name 变量对应的值的后 2 个字符?
    print('8:',name[-2:])
    # 9)    请输出 name 变量对应的值中 “e” 所在索引位置?
    print('9:',name.find('e'))
    # 10)    获取子序列,去掉最后一个字符。如: oldboy 则获取 oldbo。
    print('10:',name[:len(name) - 1])
    

    The result:

    1: aleX
    2: False
    3: True
    4:  apeX
    5: [' a', 'eX']
    6: a
    7:  al
    8: eX
    9: 3
    10:  ale
    
    Process finished with exit code 0
  4. Suppose a file test.txt, there are the following l = [{ 'name': 'alex', 'age': 84}, { 'name': 'oldboy', 'age': 73}, { 'name ':' egon ',' age ': 18},]

    Requirements: 1 reads the contents of the file 2. calculate the sum of these three individuals age

    '''
    4. 假设有一个文件test.txt,内有如下内容
    l=[
        {'name':'alex','age':84},
        {'name':'oldboy','age':73},
        {'name':'egon','age':18},
    ]
    需求:
    1. 读取文件内容
    2. 计算这三个人的年龄总和
    '''
    with open('test4.txt', 'r', encoding='utf-8')as f:
        f = f.read()
        f = f.lstrip('l=')
        f_list = eval(f)
    
        age_count = 0
        for i in f_list:
            age_count = age_count + i['age']
    
    print(age_count)

    The result:

    175
    
    Process finished with exit code 0

Guess you like

Origin www.cnblogs.com/liveact/p/11448383.html