day6 list job

1. Basic questions

  1. Given a list of numbers, print all odd numbers in the list
scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66, 1000]
for i in scores:
    if i % 2 != 0:
        print(i)
  1. Given a list of numbers, print all the numbers in the list that can be divided by 3 but cannot be divided by 2
scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66, 1000]
for i in scores:
    if i % 2 == 0 and i % 3 != 0:
        print(i)
  1. Given a list of numbers, calculate the sum of all even numbers
scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66, 1000]
sum_score = 0
for i in scores:
    if i % 2 == 0:
        sum_score += i
print(sum_score)
  1. A list of numbers is known, and the number of tens in the list is 1
scores = [45, 60, 89, 30, 12, 59, 119, 80, 71, 616, 1010]
count = 0
for i in scores:
    if i % 100 // 10 == 1:
        count += 1

print(count)
  1. 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]
list2 = []
for index, item in enumerate(list1):
    if index % 2 != 0:
        list2.append(item)

print(list2)
  1. 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]
for index, item in enumerate(nums):
    item *= 2
    nums[index] = item
    
print(nums)
  1. 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 = [10, 2, 6, 12]
# nums = [10, 2, 6, 12, 10]

if len(nums) % 2 == 0:
    print(nums[len(nums) // 2 - 1], nums[len(nums) // 2])
else:
    print(nums[len(nums) // 2])
  1. 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 i in list1:
    if type(i) == int:
        list2.append(i)

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]
count = []
for i in scores:
    if i >= 60:
        count.append(i)

print(count)
  1. 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']
i = 0

while i < len(names):
    if names[i] in names[:i]:
        names.pop(i)
    else:
        i += 1

print(names)

  1. Given a list of numbers, get the element with the largest value in the list (the max function cannot be used)
scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66, 1000]
max_scores = 0
for i in range(len(scores)):
    if scores[max_scores] < scores[i]:
        max_scores = i

print(scores[max_scores])
  1. 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]
marge_list = list1 + list2
for i in range(len(marge_list)):
    while marge_list[i - 1] > marge_list[i] and i - 1 >= 0:
        marge_list[i], marge_list[i - 1] = marge_list[i - 1], marge_list[i]
        i -= 1

print(marge_list)
  1. 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]
nums = int(input('Please enter any number:'))
for i in range(len(list1)):
    if list1[i] > nums:
        list1.insert(i, nums)
        break

print(list1)

Keywords: Python Algorithm

Added by Cleibe on Mon, 21 Feb 2022 15:33:05 +0200