day6 list job

1. Basic questions

  1. Given a list of numbers, print all odd numbers in the list

    list1 = [1,2,3,4,5]
    for x in list1:
        if x %2 != 0:
            print(x)
    
  2. Given a list of numbers, print all the numbers in the list that can be divided by 3 but cannot be divided by 2

    nums = [23, 45, 78, 90, 8, 21]
    for x in nums:
        if x % 3 == 0 and x % 2 != 0:
            print(x)
    
  3. Given a list of numbers, calculate the sum of all even numbers

    result = 0
    list1 = [1,2,3,4,5]
    for x in list1:
        if x %2 == 0:
            result += x
    print(result)
    
  4. A list of numbers is known, and the number of tens in the list is 1

    count = 0
    list1 = [11,21,32,18,71]
    for x in list1:
        if x//10%10 == 1:
            count +=1
    print(count)
    
  5. Given a list, get all elements in the list whose subscripts are odd (subscript values starting from 0)

    For example: list1 = [10, 20, 5, 34, 90, 8]

    Result: [20, 34, 8]

    list1 = [10,20,5,34,90,8]
    print(list1[1::2])
    
  6. Given a list of numbers, multiply all elements in the list by 2

    For example: num = [10, 3, 6, 12] multiplied by 2: num = [20, 6, 12, 24]

    nums = [10,3,6,12]
    new_nums = []
    for x in nums:
        new_nums.append(x*2)
    print(new_nums)
    
  7. Given a list, get the central element of the list

    For example: num = [10, 2, 6, 12] - > the central elements are: 2 and 6

    Num = [10, 2, 6, 12, 10] - > the central element is: 6

    nums = [1,2,3,4,5,6,7]
    count = len(nums)
    if len(nums) % 2 ==0:
        print(nums[len(nums)//2-1] and nums[len(nums)//2])
    else:
        print(nums[len(nums)//2])
    
  8. Given a list, get all integer elements in the list

    For example: list1 = [10, 1.23, 'abc', True, 100, 'hello', '20', 5]

    The result is: [10, 100, 5]

    list1 = [10,1.23,'abc',True,100,'hello','20',5]
    list2 = []
    for x in list1:
        if type(x) == int:
            list2.append(x)
    print(list2)
    

2. Advanced questions

  1. Define a list, save the scores of multiple students, and delete the values below 60 in the list

    For example: scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] after deletion: scores = [60, 89, 99, 80, 71, 66]

    scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
    new_scores = scores.copy()
    for x in new_scores:
        if x <60:
            scores.remove(x)
    print(scores)
    
  2. It is known that a list holds the names of multiple students. It is required to remove the duplicate names in the list

    For example: names = ['Xiao Ming', 'Zhang San', 'Li Si', 'Zhang San', 'Zhang San', 'Xiao Ming', 'Wang Wu', 'Wang Wu']

    After weight removal: names = ['Xiao Ming', 'Zhang San', 'Li Si', 'Wang Wu']

    names=['Xiao Ming','Zhang San','Li Si','Zhang San','Zhang San','Xiao Ming','Wang Wu','Wang Wu']
    result = set(names)
    print(result)
    
  3. Given a list of numbers, get the element with the largest value in the list (the max function cannot be used)

    nums = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
    max_nums = nums[0]
    for x in nums[1:]:
        if x > max_nums:
            max_nums = x
    print(max_nums)
    
    result = max(nums)
    print(result)
    
  4. It is known that there are two sequential tables (the elements in the list have been arranged in order from small to large). It is required to merge the two lists. After merging, the elements are still sorted from small to large

    For example: list1 = [10, 23, 39, 41, 52, 55, 80] list2 = [9, 38, 55, 70]

    Combined results: [9, 10, 23, 38, 39, 41, 52, 55, 55, 70, 80]

    list1 = [10, 23, 39, 41, 52, 55, 80]
    list2 = [9, 38,  55, 70]
    list3 = list2 + list1
    result = sorted(list3)
    print(result)
    
  5. Given an ordered number list (from small to large), enter any number and insert the entered number into the list. It is required that the list still maintains the sorting relationship from small to large after insertion

    For example: list1 = [10, 23, 45, 67, 91] input: 50 - > LIST1 = [10, 23, 45, 50, 67, 91]

    list1 = [10, 23, 45, 67, 91]
    num = int(input('Number entered:'))
    for index,item in enumerate(list1):
        if num < item:
            list1.insert(index,num)
            break
    else:
        list1.append()
    print(list1)
    

Keywords: Python

Added by sfw5000 on Sun, 27 Feb 2022 13:28:01 +0200