简体   繁体   English

如何在 Python 的列表中的两个元素之间找到缺失的元素?

[英]How to find missing elements between two elements in a list in Python?

I have a list as follows:我有一个列表如下:

['1', '5', '6', '7', '10']

I want to find the missing element between two elements in the above list.我想找到上面列表中两个元素之间缺少的元素。 For example, I want to get the missing elements between '1' and '5' , ie '2' , '3' and '4' .例如,我想获取'1''5'之间缺少的元素,即'2''3''4' Another example, there are no elements between '5' and '6' , so it doesn't need to return anything.另一个例子, '5''6'之间没有元素,所以它不需要返回任何东西。

Following the list above, I expect it to return a list like this:按照上面的列表,我希望它返回这样的列表:

['2', '3', '4', '8', '9']

My code:我的代码:

# Sort elements in a list
input_list.sort()

# Remove duplicates from a list
input_list = list(dict.fromkeys(input_list))

How to return the above list?如何返回上面的列表? I would appreciate any help.我将不胜感激任何帮助。 Thank you in advance!先感谢您!

If you iterate in pairs, it is easy to detect and fill in the gaps:如果成对迭代,很容易检测并填补空白:

L = ['1', '5', '6', '7', '10']
result = []
for left, right in zip(L, L[1:]):
    left, right = int(left), int(right)
    result += map(str, range(left + 1, right))

I would use the range() function to generate the missing numbers, and maybe use itertools.pairwise() to easily compare to the previous number.我会使用range() function 来生成缺失的数字,并可能使用itertools.pairwise()轻松地与之前的数字进行比较。 Since Python 3.10, pairwise is better than zip(arr, arr[1:]) because pairwise is implemented in the C layer, and does not make a copy of lists.从 Python 3.10 开始, pairwisezip(arr, arr[1:])更好,因为 pairwise 是在 C 层实现的,不会复制列表。

import itertools
arr = ['1', '5', '6', '7', '10']

new_arr = []

for p, c in itertools.pairwise(arr):
    prev, curr = int(p), int(c)
    if (prev + 1) != curr:
        new_arr.extend([str(i) for i in range(prev + 1, curr)])

If performance is not an issue, you can use a simple nested for loop:如果性能不是问题,您可以使用简单的嵌套for循环:

input_list = ['1', '5', '6', '7', '10']

missing_list = []

for ind in range(0, len(input_list)-1):

    el_0 = int(input_list[ind])
    el_f = int(input_list[ind+1])

    for num in range(el_0 + 1, el_f):
        missing_list.append(str(num))

The above assumes that the numbers are integers.以上假设数字是整数。 The first loop is something not recommended - the loop iterates using the length of the list instead of constructs like enumerate .第一个循环是不推荐的——循环使用列表的长度而不是像enumerate这样的结构来迭代。 I used it here for its simplicity.我在这里使用它是为了它的简单性。

You could use a combination of range and set and skip iteration altogether.您可以结合使用rangeset并完全跳过迭代。

#original data
data = ['1', '5', '6', '7', '10']

#convert to list[int]
L    = list(map(int, data))

#get a from/to count
R    = range(min(L), max(L)+1)

#remove duplicates and convert back to list[str]
out  = list(map(str, set(R) ^ set(L)))

print(out)

Using a classic for loop and range() method and also for one-liner fans:使用经典的 for 循环和range()方法以及单线风扇:

arr = ['1', '5', '6', '7', '10']
ans = []

for i in range(len(arr) - 1): ans += map(str, list(range(int(arr[i]) + 1, int(arr[i + 1]))))
    
print(ans)

outputs: ['2', '3', '4', '8', '9']输出: ['2', '3', '4', '8', '9']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM