Hướng dẫn missing element in list python - phần tử bị thiếu trong danh sách python

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ỡ.
We can perform the task of finding missing elements using the range function to get the maximum element fill and then insert the elements if there is a miss.

test_list =

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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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

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 : 
" ,res)
9 Vấn đề này cũng có thể được thực hiện bằng cách sử dụng các thuộc tính của sự khác biệt của tập hợp và sau đó nhận các phần tử bị thiếu trong một phạm vi.
This problem can also be performed using the properties of difference of set and then getting the elements that are missing in a range.

test_list =

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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 : 
" ,res)
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 đa

Chú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 : 
" ,res)

Đầu ra

Chạ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

Chú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 : 
" ,res)

Đầu ra

Chạ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]

Hướng dẫn missing element in list python - phần tử bị thiếu trong danh sách python

với thiết lập

  • Chú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.
  • Cập nhật ngày 26 tháng 8 năm 2020 08:08:57
  • Câu hỏi và câu trả lời liên quan
  • Chương trình tìm số thiếu kth từ danh sách các yếu tố trong Python
  • Tìm số còn thiếu trong phạm vi danh sách được sắp xếp trong Python
  • Tìm các phần tử bị thiếu của một phạm vi trong C ++
  • Tìm tất cả các yếu tố được tính trong danh sách trong Python
  • Chương trình tìm số còn thiếu từ hai danh sách các số trong Python
  • Tìm tổng các yếu tố trong danh sách trong chương trình Python
  • Tìm các yếu tố phổ biến trong danh sách các danh sách trong Python
  • Chương trình Python để tìm tổng các phần tử trong danh sách
  • Chương trình PHP để tìm các phần tử bị thiếu từ một mảng
  • Xóa các yếu tố danh sách trong Python
  • Tìm các yếu tố danh sách bắt đầu bằng chữ cái cụ thể trong Python
  • Chương trình tìm danh sách các phần tử bình phương theo thứ tự được sắp xếp trong Python
  • Thiếu hoán vị trong danh sách trong C ++