美文网首页
列表 作业

列表 作业

作者: Yehao_ | 来源:发表于2018-07-21 11:26 被阅读0次

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

    tags = ['A', 'b', 'cc', 'loli', 'G']
    length = len(tags)
    
    if length == 0:
        print('空列表')
    elif length % 2 == 0 and length != 0:
        print(tags[length / 2], tags[length / 2 + 1])
    else:
        
        print(tags[int(length/2)])
    
    Output:
    中心元素为: cc
    

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

    numbers = [15, 52, 31, 66, 7889, 134, 10]
    sum1 = 0
    for number in numbers:
        sum1 += number
    print(sum1)
    
    Output:
    8197
    

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

    classmates = ['MJ', 'Cook', 'Kobe', 'Durant', '马云']
    print(classmates[1::2])
    
    Output:
    ['Cook', 'Durant']
    

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

    numbers = [15, 20, 89, 97, 67, 76, 43]
    for number in numbers:
        if number % 2:
            print(number)
    
    Output:
    15
    89
    97
    67
    43
    

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

    numbers = [15, 20, 88, 97, 66, 76, 43]
    print(numbers)
    numbers_multiple_two = []
    for number in numbers:
        number *= 2
        numbers_multiple_two.append(number)
    print(numbers_multiple_two)
    
    Output:
    [15, 20, 88, 97, 66, 76, 43]
    [30, 40, 176, 194, 132, 152, 86]
    

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

    sum1 = 0
    numbers = [88, 78, 65, 46, 50]
    for number in numbers[1:]:
        sum1 += number 
    numbers[0] = numbers[0] + sum1
    print(numbers[0])
    
    Output:
    327
    

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

    list_A = [0, 1, 2, 3, 4, 5, 6]
    list_B = list_A[::2]
    list_C = list_A[1::2]
    print('list_A:', list_A)
    print('list_B:', list_B)
    print('list_C:', list_C)
    
    Output:
    list_A: [0, 1, 2, 3, 4, 5, 6]
    list_B: [0, 2, 4, 6]
    list_C: [1, 3, 5]
    

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

    list_A = [0, 1, 2, 3, 4, 5, 6]
    list_B = list_A[:5]
    print('list_A:', list_A)
    print('list_B:', list_B)
    
    Output:
    list_A: [0, 1, 2, 3, 4, 5, 6]
    list_B: [0, 1, 2, 3, 4]
    

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

    sum_from_left_top_to_right_bottom = 0
    sum_from_left_bottom_to_right_top = 0
    numbers = [[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],
               ]
    # print(numbers[1][2])
    for i in range(len(numbers)):
        for j in range(len(numbers[i])):
            if i == j:
                sum_from_left_top_to_right_bottom += numbers[i][j]
            if i + j == 5:
                sum_from_left_bottom_to_right_top += numbers[i][j]
    print('左上到右下:', sum_from_left_top_to_right_bottom)
    print('左下到右上:', sum_from_left_bottom_to_right_top)
    
    Output:
    左上到右下: 111
    左下到右上: 111
    

    10.有一个长度是10的列表,列表内有10个不重复的数字,要求从大到小排序。

    numbers = [54, 55, 98, 35, 0, 5, 78, 68, 64, 33]
    numbers.sort(reverse=True)
    print(numbers)
    
    Output:
    [98, 78, 68, 64, 55, 54, 35, 33, 5, 0]
    

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

    numbers = [54, 55, 98, 35, 0, 5, 78, 68, 64, 33]
    print(numbers)
    numbers.pop(2)
    print(numbers)
    
    Output:
    [54, 55, 98, 35, 0, 5, 78, 68, 64, 33]
    [54, 55, 35, 0, 5, 78, 68, 64, 33]
    

    12.有一个长度是10的列表,按递增排列,用户输入一个数,插入适当位置。

    numbers = [54, 55, 98, 35, 0, 5, 78, 68, 64, 33]
    numbers.sort()
    print(numbers)
    number = int(input('请输入数字:'))
    pos = int(input('请输入插入的位置:'))
    numbers.insert(pos-1, number)
    print(numbers)
    
    Output:
    [0, 5, 33, 35, 54, 55, 64, 68, 78, 98]
    请输入数字:1
    请输入插入的位置:2
    [0, 1, 5, 33, 35, 54, 55, 64, 68, 78, 98]
    

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

    names = ['Bob','Bob','Cook','马云', '柳传志', '马化腾', '丁磊','马云', '马化腾','柳传志']
    print(names)
    names = list(set(names))
    print(names)
    
    Output:
    ['Bob', 'Bob', 'Cook', '马云', '柳传志', '马化腾', '丁磊', '马云', '马化腾', '柳传志']
    ['马云', 'Cook', '柳传志', 'Bob', '马化腾', '丁磊']
    

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

    list_A = [54, 55, 98, 35, 11, 51, 78, 68, 64, 33]
    print(list_A)
    del list_A[4:6]
    print(list_A)
    
    Output:
    [54, 55, 98, 35, 11, 51, 78, 68, 64, 33]
    [54, 55, 98, 35, 78, 68, 64, 33]
    

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

    list_A = [0, 1, 2, 3, 2, 5, 1]
    print('A列表:', list_A)
    list_B = [1, 10, 9, 9]
    print('B列表:', list_B)
    list_C = list(set(list_A + list_B))
    print('C列表:', list_C)
    
    Output:
    A列表: [0, 1, 2, 3, 2, 5, 1]
    B列表: [1, 10, 9, 9]
    C列表: [0, 1, 2, 3, 5, 9, 10]
    

    相关文章

      网友评论

          本文标题:列表 作业

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