Python 查找值是否在X中彼此分开
假设我有一组数字Python 查找值是否在X中彼此分开,python,python-2.7,statistics,Python,Python 2.7,Statistics,假设我有一组数字numbers=[70,68,45,55,47,50,69],排序后的数字(为了澄清)45,47,50,55,68,69,70 我希望根据我正在处理的数据集,在指定的范围内找到最大的一组数字。让我们假设,为了论证,我想找到彼此之间在3以内的所有数字 因此,使用上述输入,我的程序的输出将是:[70,68,69],因为这是最大的一组数字。如果我有多个相等的列表,我会选择数字最高的列表 我已经写了一个程序,解决了我试图实现的目标(或者似乎是,因为我无法正确地看到我的程序的例外):我向你
numbers=[70,68,45,55,47,50,69]
,排序后的数字(为了澄清)45,47,50,55,68,69,70
我希望根据我正在处理的数据集,在指定的范围内找到最大的一组数字。让我们假设,为了论证,我想找到彼此之间在3以内的所有数字
因此,使用上述输入,我的程序的输出将是:[70,68,69]
,因为这是最大的一组数字。如果我有多个相等的列表,我会选择数字最高的列表
我已经写了一个程序,解决了我试图实现的目标(或者似乎是,因为我无法正确地看到我的程序的例外):我向你寻求的是:
- 链接到…的网页
- 姓名
- (或直接帮助)
。。。数学概念帮助我改进算法的这一部分。我觉得我只是在做一些有效的事情,但这真的,真的不是最佳的和,看起来不好看。我不是在寻找最优化的算法,但我想以更简单、更雄辩的方式来完成这项任务
证明我做了这项工作:
import numpy as np
def find_matching_numbers(numbers):
tempList = [[numbers[0]]]
for value in numbers[1:]:
for x in tempList:
# tempMean : average of current list
tempMean = np.mean(x)
# if the number is not alone, we're searching from
# the average of the list, in both directions (min and max)
# with a range of half our initial range search
# else if the number is alone, we're searching as if it is
# considered as the min AND the max of the range.
if len(x) > 1 and value >= tempMean - 1.5 and value <= tempMean + 1.5 or\
len(x) == 1 and value >= tempMean - 3 and value <= tempMean + 3:
x.append(value)
# Adding the value to the end of our rangesList,
# since it could be a useful value in another context with
# other datas.
tempList.append([value])
# Keeping lists with highest length.
maxLen = max([len(items) for items in tempList])
ret = []
for items in tempList:
if len(items) == maxLen:
ret.append(items)
# If multiple lists with highest length, keep highest one.
if type(ret[0]) == type([]) and len(ret) > 1:
ret = max(ret)
return ret
将numpy导入为np
def查找匹配的数字(数字):
圣堂武士=[[数字[0]]]
对于数字[1:]中的值:
对于圣殿骑士团中的x:
#tempMean:当前列表的平均值
tempMean=np.平均值(x)
#如果这个号码不是唯一的,我们是从
#列表在两个方向上的平均值(最小值和最大值)
#范围为初始搜索范围的一半
#否则,如果号码是单独的,我们会像搜索一样搜索
#视为范围的最小值和最大值。
如果len(x)>1且值>=tempMean-1.5且值=tempMean-3且值为1:
ret=最大值(ret)
回程网
Python3,但我认为要使其适用于Python2,您需要做的就是从print语句中删除括号
# uderscore for the _range input so we don't override the builtin range() function
def find_matching_numbers(numbers, _range=3):
numbers = sorted(numbers)
# sorting first allows us a faster algorithm because we know the range
# is our current number - first in the list
lists = []
for num in numbers:
lists.append([num])
# don't look at the last list because we just added this number
for l in lists[:-1]:
if num - l[0] <= _range:
l.append(num)
# timsort is stable, so the "largest" list will be last
return sorted(lists, key=len)[-1]
numbers = [70, 68, 45, 55, 47, 50, 69]
print(find_matching_numbers(numbers))
# [68, 69, 70]
visited = set()
for i in range(1, max(numbers) - min(numbers) + 1):
n = find_matching_numbers(numbers, i)
if sum(n) not in visited:
visited.add(sum(n))
print(i, n)
# 1 [69, 70]
# 2 [68, 69, 70]
# 10 [45, 47, 50, 55]
# 15 [55, 68, 69, 70]
# 20 [50, 55, 68, 69, 70]
# 23 [47, 50, 55, 68, 69, 70]
# 25 [45, 47, 50, 55, 68, 69, 70]
用于_range输入的uderscore,这样我们就不会重写内置range()函数
def查找匹配的编号(编号,范围=3):
数字=已排序(数字)
#排序首先允许我们使用更快的算法,因为我们知道范围
#是我们目前的号码-在列表中的第一名吗
列表=[]
对于数字中的num:
lists.append([num])
#不要看最后一个列表,因为我们刚刚添加了这个数字
对于列表中的l[:-1]:
如果num-l[0]最长[1]:
最长=(num,ll)
如果num-l[0],这是一个合理的问题,但这可能不是合适的论坛。也许可以尝试一下像Stack Overflow这样的计算机科学板?因为这更像是一个数学问题而不是一个计算问题,所以我认为它在这里更合适,但如果你认为这个问题应该出现在Stack Overflow上,我会在这里发布几次。:)这是一个计算问题。我喜欢你解决这个问题的方式。我很嫉妒(还有点惭愧),我自己也没想过。
def find_matching_numbers2(numbers, _range=3):
numbers = sorted(numbers) # allows us a faster algorithm
lists = []
longest = (0, 1)
for num in numbers:
remove = set()
lists.append([num])
# don't look at the last list because we just added this number
for l in lists[:-1]:
ll = len(l)
if ll > longest[1]:
longest = (num, ll)
if num - l[0] <= _range:
l.append(num)
elif ll < longest[1]:
remove.add(l[0])
lists = [l for l in lists if l[0] not in remove]
# timsort is stable, so the "largest" list will be last
return sorted(lists, key=len)[-1]