打印在给定一组参考距离值时最小距离发生的时间。

huangapple go评论84阅读模式
英文:

Print time at which minimum distance occurs given a set of reference distance values

问题

从列表A中的一组值中,我想知道与列表B中的值的最小距离发生的时间(第一列),这些最小距离在第二列中报告。

import numpy as np

list_A = np.loadtxt('listA.dat')
file_B = np.loadtxt('listB.dat')

distances = file_B[:,1]
time = file_B[:,0]

abs_differences = []

for point in list_A:
    diffs = []
    for distance in distances:
        diffs.append(abs(distance - point))
    min_index = np.argmin(diffs)
    min_time = time[min_index]
    abs_differences.append(min_time)

with open("output.txt", "w") as f:
    for min_time in abs_differences:
        print(min_time, file=f)

然而,输出文件需要很长时间才能写入,因为对于数据集中的每个点,都会打印出列表B中的每个点发生最小距离的时间,因此会有多次出现时间值。是否有解决这个问题的方法,可能可以简化代码?

请注意,我已经简化了代码并修复了其中的错误。现在,它将仅打印每个点的最小时间值到输出文件中,而不是多次打印。

英文:

From a list of values in list A, I want to know the time (first column) for a list B in which the minimum distances with values of the list A occur (distances reported in second column).

import numpy

list_A = np.loadtxt('listA.dat')
file_B = np.loadtxt('listB.dat')

distances = file_B[:,1]
time = file_B[:,0]

abs_differences = []

for point in list_A:
    diffs = []
    for distance in distances:
        diffs.append(abs(distance-point))
        list={}
        list=['Point'] = point
        list=['AbsDiff'] = diffs
        abs_differences.append(list)

for ele in abs_differences:
   diffs = ele['AbsDiff']
   index = diffs.index(min(diffs))
   with open("output.txt","a") as f:
       print(time[index], file=f)

However, the output file takes a really long time to be written, since the time at which the minimum distance occurs for every point of list B is printed for every point of the dataset, therefore having multiple occurrences of the time values. Is there a way to solve this issue, possibly simplifying the code?

答案1

得分: 1

您在代码中多次切换类型,并将Python关键字list用作变量名,因此至少出现了这两个原因,您的代码无法正常工作。

您可以尝试基于以下简化的代码:

import numpy as np

list_A = np.loadtxt('listA.dat')
file_B = np.loadtxt('listB.dat')

list_B = file_B[:, 1]
times = file_B[:, 0]

min_times = []  # 元组列表(pointA,pointB,indexB,distance,time)
for pointA in list_A:
    min_distance = float('inf')
    min_time = None

    for indexB, pointB in enumerate(list_B):
        distance = abs(pointA - pointB)
        if distance < min_distance:
            min_distance = distance
            min_time = times[indexB]
    min_times.append((pointA, pointB, indexB, min_distance, min_time))

print(min_times)

# 如果您想要距离:(元组中的索引3)
print([mt[3] for mt in min_times])

# 一旦确定所需的结果,您还可以将其写入文件

请注意,上述代码已经根据您提供的简化版本进行了翻译。

英文:

You are switching between types several times and using the python keyword list as a variable name, so your code cannot work properly at least for these two reasons.

You should try something based on the following simplified code:

import numpy as np

list_A = np.loadtxt(&#39;listA.dat&#39;)
file_B = np.loadtxt(&#39;listB.dat&#39;)

list_B = file_B[:,1]
times = file_B[:,0]

min_times = []  # list of tuples (pointA, pointB, indexB, distance, time)
for pointA in list_A:
    min_distance = float(&#39;inf&#39;)
    min_time = None
    
    for indexB, pointB in enumerate(list_B):
        distance = abs(pointA - pointB)
        if distance &lt; min_distance:
            min_distance = distance
            min_time = times[indexB]
    min_times.append((pointA, pointB, indexB, min_distance, min_time))
    

print(min_times)

# if you want the distances: (index 3 in the tuple)
print([mt[3] for mt in min_times])

# you can also write to file once you are sure of the result wanted

huangapple
  • 本文由 发表于 2023年6月19日 17:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定