美文网首页
Day4作业-列表相关

Day4作业-列表相关

作者: 周zau1 | 来源:发表于2018-07-21 11:49 被阅读84次

    1.已知⼀个列表,求列表中⼼元素。

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    num = len(A)
    n = int(num / 2)
    print(A[n])
    
    5
    Process finished with exit code 0
    

    2.已知⼀个列表,求所有元素和。

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    num = 0
    for x in A:
        num += x
    print(num)
    
    45
    Process finished with exit code 0
    

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

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    num = len(A)
    print(A[1:num:2])
    
    [2, 4, 6, 8]
    Process finished with exit code 0
    

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

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    for x in A:
        if x % 2 != 0:
            print(x)
            continue
    
    1
    3
    5
    7
    9
    Process finished with exit code 0
    

    5.已知⼀个列表,将所有元素乘⼆。

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    B = []
    for x in A:
        x *= 2
        B.append(x)
    print(B)
    
    [2, 4, 6, 8, 10, 12, 14, 16, 18]
    Process finished with exit code 0
    

    6.已知⼀个列表,将所有元素加到第⼀个元素中。

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    B = [A[0]] + A
    A[0] = B
    print(A)
    
    [[1, 1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 3, 4, 5, 6, 7, 8, 9]
    Process finished with exit code 0
    

    7.已知⼀个列表A,将奇数位置元素存到B列表中,偶数元素存到C列表中。

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    B = []
    C = []
    for x in range(len(A)):
        if x % 2 == 0:
            B.append(A[x])
        else:
            C.append(A[x])
    print(B ,C)
    
    [1, 3, 5, 7, 9] [2, 4, 6, 8]
    Process finished with exit code 0
    

    8.把A列表的前5个元素复制到B列表中。

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    B = A[0:5]
    print(B)
    
    [1, 2, 3, 4, 5]
    Process finished with exit code 0
    

    9.把1----36分别放⼊列表中,计算列表数组对⻆元素之和。6 * 6的列表

    sum1 = 0
    for x in range(1, 37):
        print(x, end=' ')
        if x < 10:
            print('', end=' ')
        if x % 6 == 0:
            print('')
        sum1 += x
    sum2 = sum1 / (6 / 2)
    print('对角线和为:' % sum2)
    
    1  2  3  4  5  6  
    7  8  9  10 11 12 
    13 14 15 16 17 18 
    19 20 21 22 23 24 
    25 26 27 28 29 30 
    31 32 33 34 35 36 
    对角线和为: 222.0
    

    10.有⼀个⻓度是10的列表,列表内有10个不重复的数字,要求按从⼤到⼩排序。

    A = [21, 95, 2, 64, 35, 16, 81, 51, 24, 8]
    A.sort()
    print(A[::-1])
    
    [95, 81, 64, 51, 35, 24, 21, 16, 8, 2]
    Process finished with exit code 0
    

    11.有⼀个⻓度是10的列表,要求删除某⼀个位置的元素

    A = [21, 95, 2, 64, 35, 16, 81, 51, 24, 8]
    n = int(input('请输入需要删除的元素的位置:'))
    max1 = len(A)
    if n <= max1:
        B = A.pop(n - 1)
        print('删除的元素为:', B)
    else:
        print('输入无效')
    
    请输入需要删除的元素的位置:8
    删除的元素为: 51
    Process finished with exit code 0
    

    12.有⼀个⻓度是10的列表,按递增排列,⽤户输⼊⼀个数,插⼊适当位置。

    A = [2, 8, 16, 21, 24, 35, 51, 64, 81, 95]
    num = int(input('请输入一个整数:'))
    A.append(num)
    A.sort()
    print(A)
    
    请输入一个整数:8
    [2, 8, 8, 16, 21, 24, 35, 51, 64, 81, 95]
    Process finished with exit code 0
    

    13.有⼀个⻓度是10的列表,数组内有10个⼈名,要求去掉重复的

    names = ['费舍尔', '加索尔', '科比', '奥多姆', '拜纳姆', '费舍尔', '加索尔', '科比', '奥多姆', '拜纳姆']
    names1 = list(set(names))
    print(names1)
    
    ['费舍尔', '奥多姆', '加索尔', '科比', '拜纳姆']
    Process finished with exit code 0
    

    14.把A列表的第三到第六位之间的元素删除。

    A = [21, 95, 2, 64, 35, 16, 81, 51, 24, 8]
    del A[3: 5]
    print(A)
    
    [21, 95, 2, 16, 81, 51, 24, 8]
    Process finished with exit code 0
    

    15.已知A列表,B列表,定义⼀个列表C,要求C包含A,B数组中的数据(⽆重复值)。

    A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    B = [21, 95, 2, 64, 35, 16, 81, 51, 24, 8]
    C = list(set(A + B))
    print(C)
    
    [64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 35, 16, 81, 51, 21, 24, 95]
    Process finished with exit code 0
    

    相关文章

      网友评论

          本文标题:Day4作业-列表相关

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