Cải thiện bài viết Lưu bài viết Đôi khi, chúng ta có thể nhận được các phần tử trong phạm vi làm đầu vào nhưng một số giá trị bị thiếu trong phạm vi liên tiếp khác. Chúng ta có thể có một trường hợp sử dụng trong đó chúng ta cần có được tất cả các yếu tố bị thiếu. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện. Phương pháp số 1: Sử dụng danh sách hiểu chúng tôi có thể thực hiện nhiệm vụ tìm các phần tử bị thiếu bằng cách sử dụng hàm phạm vi để nhận phần tử tối đa và sau đó chèn các phần tử nếu có bỏ lỡ.
The original list : [3, 5, 6, 8, 10] The list of missing elements : [0, 1, 2, 4, 7, 9]9 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :0 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :1 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :2 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :3 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :4 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :5 = listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :7 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :8 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :9 Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [0, 2, 3, 4, 8, 9, 10, 12, 13]0 Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [0, 2, 3, 4, 8, 9, 10, 12, 13]1__ The original list : [3, 5, 6, 8, 10] The list of missing elements : [0, 1, 2, 4, 7, 9]9 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :0 listA = [1,5,6, 7,11,14] # printing original list print("Given list : ",listA) # using set res = list(set(range(max(listA) + 1)) - set(listA)) # Result print("Missing elements from the list :5 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :2 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :3 listA = [1,5,6, 7,11,14] # printing original list print("Given list : ",listA) # using set res = list(set(range(max(listA) + 1)) - set(listA)) # Result print("Missing elements from the list :8 Đầu ra: The original list : [3, 5, 6, 8, 10] The list of missing elements : [0, 1, 2, 4, 7, 9] Phương pháp số 2: Sử dụng
The original list : [3, 5, 6, 8, 10] The list of missing elements : [0, 1, 2, 4, 7, 9]9 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :0 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :1 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :2 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :3 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :4 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :5 = listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :7 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :8 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :9 Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [0, 2, 3, 4, 8, 9, 10, 12, 13]0 Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [0, 2, 3, 4, 8, 9, 10, 12, 13]1__ The original list : [3, 5, 6, 8, 10] The list of missing elements : [0, 1, 2, 4, 7, 9]9 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :0 listA = [1,5,6, 7,11,14] # printing original list print("Given list : ",listA) # using set res = list(set(range(max(listA) + 1)) - set(listA)) # Result print("Missing elements from the list :5 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :2 listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list :3 listA = [1,5,6, 7,11,14] # printing original list print("Given list : ",listA) # using set res = list(set(range(max(listA) + 1)) - set(listA)) # Result print("Missing elements from the list :8 Đầu ra: The original list : [3, 5, 6, 8, 10] The list of missing elements : [0, 1, 2, 4, 7, 9] Nếu chúng tôi có một danh sách chứa các số, chúng tôi có thể kiểm tra xem các số có tiếp giáp hay không và cũng tìm thấy số nào bị thiếu trong một loạt các số coi số cao nhất là giá trị cuối cùng. Với phạm vi và tối đaChúng ta có thể thiết kế một vòng lặp để kiểm tra sự vắng mặt của các giá trị trong một phạm vi bằng cách sử dụng toán tử không trong toán tử. Sau đó, nắm bắt tất cả các giá trị này bằng cách thêm chúng vào một danh sách mới trở thành tập kết quả. Thí dụ& nbsp; bản demo trực tiếp listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) # using range and max res = [ele for ele in range(max(listA) + 1) if ele not in listA] # Result print("Missing elements from the list : Đầu raChạy mã trên cho chúng ta kết quả sau - Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [0, 2, 3, 4, 8, 9, 10, 12, 13] với thiết lậpChúng tôi áp dụng chức năng tập hợp để giữ tất cả các giá trị duy nhất cho một phạm vi đã cho và sau đó trừ danh sách đã cho khỏi nó. Vì vậy, điều này cung cấp cho tập kết quả chứa các giá trị bị thiếu từ các số tiếp giáp. Thí dụ& nbsp; bản demo trực tiếp listA = [1,5,6, 7,11,14] # printing original list print("Given list : ",listA) # using set res = list(set(range(max(listA) + 1)) - set(listA)) # Result print("Missing elements from the list : Đầu raChạy mã trên cho chúng ta kết quả sau - Given list : [1, 5, 6, 7, 11, 14] Missing elements from the list : [0, 2, 3, 4, 8, 9, 10, 12, 13]
với thiết lập
|