查找二维数组元素的第一和第二元素之间的最大和最小差异



>我做了一个函数,它接受一个 2d 元素列表(包含包含 2 个元素的列表(作为参数,并返回元素差异最大的元素(或多个(元素和元素差异最小的元素(或多个(。例如,给定一个参数 ([2,8],[3,4],[2,7],[4,10](,该函数将返回:max=([2,8], [4,10](, min=[3,4]。

我做了一个函数来做到这一点,但代码相当大,没有我添加填充列表的部分,作为用户输入的参数传递,我也想这样做。

def maxminIntervals(lst):
 mx=lst[0][1]-lst[0][0]
 mn=mx
 count_max=count_min=0
 max=[]
 min=[]
 print(max,min)
 print(mx,mn)
 for element in lst:
    y=element[1]-element[0]
    if y>mx:
        max=[]
        max.append(element)
        count_max=0
        mx=y
    elif y==mx:
        max.append(element)
        mx=y
        count_max+=1
    if y<mn:
        min=[]
        min.append(element)
        count_min=0
        mn=y
    elif y==mn:
        min.append(element)
        mn=y
        count_min+=1
    print(y)
 print("Max=",end='')
 if count_max>0:
        print("(",end=" ")
 for i in max:
    print(i,end=' ')
 if count_max>0:
        print(")",end=" ")
 print("n")
 print("Min=",end=' ')
 if count_min>0:
        print("(",end=" ")
 for i in min:
    print(i,end=' ')
 if count_min>0:
    print(")",end=" ")

在我看来,代码对于 Python 来说太大了。有没有一个简单的快捷方式(内置功能等(来缩短它?

主要思想是跟踪min值和max值,但也要有单独的列表来跟踪每对

def maxMinIntervals(lst):
    maximum, minimum = [], []
    max_value, min_value = float('-inf'), float('inf')
    for pair in lst:
        value = abs(pair[1] - pair[0])
        if value > max_value:
            max_value = value
            maximum = []
            maximum.append(pair)
        elif value == max_value:
            maximum.append(pair)
        if value < min_value:
            minimum = []
            minimum.append(pair)
            min_value = value
        elif value == min_value:
            minimum.append(pair)
    return maximum, minimum 

司机

input_list = [[2,8], [3,4], [2,7], [4,10]]
max_ans, min_ans = maxMinIntervals(input_list)
print('maximum results: ', max_ans)
print('minimum results: ', min_ans)

输出

("最大结果:", [[2, 8], [4, 10]](

("最低结果:", [[3, 4]](

如果你想

保留所有的对,如果它是最大值/最小值,你可以试试这个(我已经在简化它的地方发表了评论(:

def maxminIntervals(lst):
    max_diff, min_diff = float('-inf'), float('inf')
    max_results, min_results = [], []
    # for loop and unzip pairs to num1, num2
    for num1, num2 in lst:
        # define diff to compare min and max
        diff = num2 - num1
        # append to max_results
        if diff == max_diff:
            max_results.append([num1, num2])
        # update a new max_results
        elif diff > max_diff:
            max_diff = diff
            max_results = [[num1, num2]]
        # append to min_results
        if diff == min_diff:
            min_results.append([num1, num2])
        # update a new min_results
        elif diff < min_diff:
            min_diff = diff
            min_results = [[num1, num2]]
    return max_results, min_results
def test():
    lst = ([2, 8], [3, 4], [2, 7], [4, 10])
    max_results, min_results = maxminIntervals(lst)
    print('max results:', max_results)
    print('min results:', min_results)

输出:

max results: [[2, 8], [4, 10]]
min results: [[3, 4]]

这是一个 4 行解决方案,更 pythonic,但成本更高:

from collections import defaultdict
from operator import itemgetter
def maxminIntervals2(lst):
    diff_dict = defaultdict(list)
    for pair in lst:
        diff_dict[pair[1] - pair[0]].append(pair)
    return max(diff_dict.items(), key=itemgetter(0))[1], min(diff_dict.items(), key=itemgetter(0))[1]

希望对您有所帮助,如果您有其他问题,请发表评论。

相关内容

  • 没有找到相关文章

最新更新