Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找python列表中的所有、最大和最小差异。更优化的方法_Python_Algorithm - Fatal编程技术网

查找python列表中的所有、最大和最小差异。更优化的方法

查找python列表中的所有、最大和最小差异。更优化的方法,python,algorithm,Python,Algorithm,我有一个不同数字的数组,我必须找到这些数字的最小值、最大值和所有差异。我试过这个: # list of numbers from which we want to find differences. list_of_nums = [1, 9, 7, 13, 56, 5] def find_differences(list_of_nums): list_of_nums = list(dict.fromkeys(list_of_nums)) # remove duplicates

我有一个不同数字的数组,我必须找到这些数字的最小值、最大值和所有差异。我试过这个:

# list of numbers from which we want to find differences. 
list_of_nums = [1, 9, 7, 13, 56, 5]


def find_differences(list_of_nums):
    list_of_nums = list(dict.fromkeys(list_of_nums)) # remove duplicates
    differences = [] # list to keep differences

    for i in list_of_nums:
        for j in list_of_nums:
            if i > j:
                differences.append(i - j)
            elif j > i:
                differences.append(j - i)
            else:
                continue

    differences = list(dict.fromkeys(differences)) # remove duplicates
    return sorted(differences)


differences = find_differences(list_of_nums)


print("All differences: ", differences)
print("Maximum difference: ", max(differences))
print("Minimum difference: ", differences[0])
这里的一切都正常,但在排序、删除重复项和循环列表方面浪费了大量时间。因此,列表越大,程序运行越慢。我试图用排序算法替换内置的
sorted
函数,但结果更糟。我得到的是:

All differences:  [2, 4, 6, 8, 12, 43, 47, 49, 51, 55]
Maximum difference:  55
Minimum difference:  2

如果有人知道更好的解决方法,我将不胜感激

创建一个函数,用于计算一组所有差异,并返回最小和最大差异

导入itertools
def get_min_max_dif(数字):
Diff={a在itertools.product(数字,数字)中a,b的abs(a-b)如果a!=b}
返回最小值(差异),最大值(差异)
然后使用此函数计算数字列表

打印(获取最小最大dif([1,9,7,13,56,5]))
更新:

如果要避免使用
itertools
库,可以执行以下操作:

def get_min_max_dif(数字):
diff={abs(a-b)表示a在数中,如果a!=b}
返回最小值(差异),最大值(差异)
基于来自

代码

from itertools import combinations

def find_differences(lst):
  " Find all differences, min & max difference "
  d = [abs(i - j) for i, j in combinations(set(lst), 2)]

  return min(d), max(d), d
测试

list_of_nums = [1, 9, 7, 13, 56, 5]
min_, max_, diff_ = find_differences(list_of_nums)
print(f'All differences: {diff_}\nMaximum difference: {max_}\nMinimun difference: {min_}')
All differences: [4, 6, 8, 12, 55, 2, 4, 8, 51, 2, 6, 49, 4, 47, 43]
Maximum difference: 55
Minimun difference: 2
输出

list_of_nums = [1, 9, 7, 13, 56, 5]
min_, max_, diff_ = find_differences(list_of_nums)
print(f'All differences: {diff_}\nMaximum difference: {max_}\nMinimun difference: {min_}')
All differences: [4, 6, 8, 12, 55, 2, 4, 8, 51, 2, 6, 49, 4, 47, 43]
Maximum difference: 55
Minimun difference: 2
性能

from itertools import combinations

def find_differences(lst):
  " Find all differences, min & max difference "
  d = [abs(i - j) for i, j in combinations(set(lst), 2)]

  return min(d), max(d), d
摘要-当前方法比原始方法快2倍

对已发布列表的测试

结果

0.108 seconds  # using combinations
0.274 seconds  # original post (using sort)
0.481 seconds  # this post (using combinations)
1.032 seconds  # original post (using sort)
测试随机列表(10000个元素)

结果

0.108 seconds  # using combinations
0.274 seconds  # original post (using sort)
0.481 seconds  # this post (using combinations)
1.032 seconds  # original post (using sort)

这里的区别是什么?一些较大的数字-一些较小的数字。但是,对整个列表进行排序是不必要的,因为我们检查减法是否合法。还有其他建议吗?太好了!根本不需要排序。但我认为使用外部图书馆可以补偿我们之前浪费时间的东西。它不会减慢程序的速度吗?你的代码是最快的!非常感谢。时间是0.0001179479950224494。@Muhammadrasul——更新了一篇文章,通过一些性能测试来回答您的问题。正如你所看到的,这个程序更快了。非常感谢!这正是我所需要的。但我认为使用外部库可以补偿我们之前浪费时间的事情。它不会减慢程序吗?不要猜测,在你特定的环境中测量它。幸运的是,它确实对时间有积极的影响。0.000129330999787915125表示我的代码,0.0001253070041679665表示您的代码。虽然差别不大,但确实有效。Itertools不是一个外部库。