我想编写一个代码,它将两个最接近的数字定位到给定的参数,而不使用任何模块。以下是我目前的情况。
list1 = (1,3,5,8,12)
x = 9
for value in list1:
point = list1[x - 1], list1[x + 1]
预期输出为[8,12]
答案 0 :(得分:2)
您可以使用已排序的平方dist-list来实现它。通过计算差异并平方,你可以摆脱负距离。接近目标值的东西,即
9 - x = 0 ==> squared 0
10 - x = 1 ==> squared 1
8 - x = -1 ==> squared 1
12 - x = 3 ==> squared 9 etc.
保持距离,远处的东西更加遥远。
# your "list" was a tuple - make it a list
data = [1,3,5,8,12]
x = 9
# calculate the difference between each value and your target value x
diffs = [t-x for t in data]
print(diffs)
# sort all diffs by the squared difference
diffsSorted = sorted([t-x for t in data], key = lambda x:x**2)
print(diffsSorted)
# take the lowes 2 of them
diffVals = diffsSorted[0:2]
print(diffVals)
# add x on top again
values = [t + x for t in diffVals]
print(values)
# Harder to understand, but you could reduce it into a oneliner:
allInOne=[k+x for k in sorted([t-x for t in data], key = lambda x:x**2)][:2]
print(allInOne)
输出:
[-8, -6, -4, -1, 3] # difference between x and each number in data
# [64, 36, 16, 1, 9] are the squared distances
[-1, 3, -4, -6, -8] # sorted differences
[-1, 3] # first 2 - just add x back
[8, 12] # result by stepwise
[8, 12] # result allInOne
中间步骤(未打印):
[64, 36, 16, 1, 9] # squared differences - we use that as key to
# sort [-8, -6, -4, -1, 3] into [-1, 3, -4, -6, -8]
比较heapq和list-approach的一些测量(改为abs()而不是平方):
import timeit
setup = """
import random
random.seed(42)
rnd = random.choices(range(1,100),k={})
import heapq
x = 42
n = 5 """
h = "fit = heapq.nsmallest(n, rnd, key = lambda L: abs(x - L))"
l = "allInOne=[k+x for k in sorted([t-x for t in rnd], key = lambda x:abs(x))][:n]"
rt = {}
for k in range(1,6):
s = setup.format(10**k)
rt[10**k] = (timeit.timeit(l,setup=s,number=1000),timeit.timeit(h,setup=s,number=1000))
print(rt)
输出:
# rnd-size list approch heapq
{
10: ( 0.06346651086960813, 0.11704596144812314),
100: ( 0.5278085906813885, 0.8281634763797711),
1000: ( 5.032436315978541, 7.462741343986483),
10000: ( 54.45165343575938, 79.96112521267483),
100000: (577.708372381287, 835.539905495399)
}
list总是比heapq更快,heapq(特别是对于更大的列表)更好的空间复杂性。
答案 1 :(得分:2)
试试这个:
409 Conflict
返回所需值t= [abs(i-x) for i in list1]
sorted_list = sorted(enumerate(t), key=lambda i:i[1])
(list1[sorted_list[0][0]], (list1[sorted_list[1][0]]))