我有两个数据集,组A
有数百个数字,组B
有八个数字。我想在组A
中找到另外八个与组B
中的四个数字最接近的数字。现在,我每次从B
组取一个数字,并将其与A
组进行比较。还有其他更好的方法吗?
A = [762.859793, 763.544183, 764.272883, 766.190053, 767.108693, 767.123893, 768.933893, 770.619013, 771.375173, 771.522083, 774.9478730000001, 775.138843, 775.463863, 776.332693, 776.466303, 777.650083, 778.3751129999999, 781.922783, 782.592033, 784.224423, 784.779903, 785.2743429999999, 787.098133, 787.8258030000001, 787.8593030000001, 788.194393, 790.939383, 791.379753, 793.572803, 796.093073, 796.720693, 796.852473, 798.519643, 800.145803, 800.758723, 801.239613, 801.332453, 801.819333, 805.290823, 806.404353, 807.995663, 808.683403, 810.0645030000001, 810.695673, 810.7933929999999, 811.150203, 814.270803, 814.706783, 815.6527629999999, 819.127253, 819.820733, 821.030753, 821.166193, 823.111683, 824.637013, 826.707083, 826.997243, 827.441463, 828.529713, 830.364193, 831.241203, 831.2571929999999, 832.635243, 832.9749730000001, 834.222713, 837.932063, 838.250483, 838.695363, 840.0615730000001, 841.246143, 841.300043, 841.5062230000001, 844.7620730000001, 845.1879230000001, 846.261283, 847.139523, 849.0279429999999, 850.5646929999999, 850.726553, 851.8736529999999, 853.9559830000001, 854.171933, 857.674983, 858.035633, 858.327443, 858.9393230000001, 859.365103, 860.0646929999999, 861.035573, 861.496883, 864.127743, 866.558833, 867.0207429999999, 868.135673, 869.329093, 870.5860529999999, 871.1149230000001, 871.691343, 872.156483, 875.2681230000001, 875.921803, 876.6624029999999, 879.2358929999999, 879.7697029999999, 880.6190730000001, 882.563473, 883.1651529999999, 883.6522130000001, 884.311393, 886.585383, 888.0967929999999, 889.6549630000001, 890.564143, 891.0488230000001, 892.887933, 893.3944630000001, 896.817743, 897.9441429999999, 898.577573, 898.681813, 900.280573, 901.308643, 903.324453, 905.128653, 905.9143029999999, 906.190423, 906.641533, 907.232963, 909.896413, 912.3604630000001, 913.905633, 914.0172630000001, 915.186703, 915.764243, 917.266943, 921.000943, 921.958943, 923.0551429999999, 924.235543, 924.545743, 925.517043, 925.8717429999999, 927.906943, 931.119243, 931.856543, 933.8970429999999, 933.929043, 934.2806429999999, 935.171843, 936.956243, 938.8980429999999, 941.066643, 941.633743, 942.986043, 944.437243, 945.839343, 948.351143, 949.9056429999999, 950.118243, 950.587543, 951.3468429999999, 954.000243, 954.205843, 954.944843, 955.703343, 959.0683429999999, 959.298543, 960.0447429999999, 961.379543, 965.161943, 967.108743, 967.7327429999999, 968.199043, 968.438843, 969.973543, 970.540543, 971.3676429999999, 972.284543, 973.603743, 976.753143, 978.1982429999999, 978.214943, 978.545843, 979.070643, 980.912243, 982.989643, 985.355943, 986.1942429999999, 986.3504429999999, 986.4826429999999, 988.964343, 989.596543, 992.602043, 993.117943, 993.7612429999999, 994.3083429999999, 994.401343, 995.340643, 998.5397429999999, 999.000443, 1000.593343, 1000.6357429999999, 1001.2584429999999, 1001.334243, 1003.663543, 1004.3092429999999, 1006.184043, 1006.665143, 1007.890843, 1010.872443, 1011.4528429999999, 1012.454843, 1012.648543, 1012.825443, 1013.512643, 1015.107543, 1017.5950429999999, 1018.145843, 1022.399143, 1023.348243, 1023.4881429999999, 1023.703943, 1023.892743, 1024.563343, 1026.059343, 1028.420643, 1028.633143, 1028.928743, 1029.316843, 1032.945843, 1036.954843, 1037.770843, 1038.025643, 1039.704743, 1040.994643, 1041.791843, 1042.953943, 1043.592843, 1043.708343, 1050.5268429999999, 1052.079443, 1052.649543, 1052.684943, 1055.256443, 1060.528543, 1061.940143, 1062.041143, 1065.194343, 1065.722143, 1066.910543, 1068.947743, 1073.2487429999999, 1073.632343, 1074.822043, 1075.200543, 1076.141143, 1077.981343, 1079.280243, 1081.3022429999999, 1081.893343, 1085.848643, 1086.145043, 1087.4752429999999, 1087.7197429999999, 1089.479843, 1094.0820429999999, 1094.511343, 1094.685543, 1096.808443, 1097.674643, 1100.9245429999999, 1101.318843, 1101.795743, 1106.028343, 1106.752143, 1108.441043, 1109.063443, 1110.809843, 1112.244343, 1115.205043, 1115.793543, 1116.281043, 1117.533043, 1120.772743, 1120.943343, 1122.063243, 1126.348043, 1126.588843, 1127.782343, 1127.8493429999999, 1132.284543, 1133.968643, 1134.083643, 1134.812743, 1138.425743, 1140.277243, 1142.692943, 1143.479343, 1143.508343, 1146.1264429999999, 1147.5087429999999, 1148.0667429999999, 1149.901943, 1150.083443, 1154.467843, 1155.7469429999999, 1156.378443, 1157.213743, 1157.893943, 1161.299843, 1163.574343, 1165.368743, 1166.530043, 1167.6391429999999, 1168.554843, 1169.4887429999999, 1170.586043, 1175.934543, 1178.156243, 1178.610543, 1178.921943, 1179.145843, 1179.573643, 1187.012243, 1187.262843, 1187.406943, 1189.229243, 1191.636343, 1191.886343, 1194.272843, 1195.991843, 1199.937643, 1200.047843, 1201.449543, 1204.830543, 1205.389743, 1209.462843, 1210.561743, 1212.594143, 1213.386543, 1214.1418429999999, 1214.154443, 1219.073643, 1221.015143, 1222.636843, 1223.881443, 1227.9897429999999, 1228.923443, 1229.525743, 1233.922243, 1234.473743, 1235.452343, 1235.808943, 1240.449043, 1241.093643, 1245.171843, 1245.478243, 1246.061043, 1246.452043, 1249.927343, 1252.341943, 1255.264743, 1257.632643, 1258.441243, 1258.452643, 1259.841943, 1260.042343, 1265.0739429999999, 1266.0141429999999, 1267.6002429999999, 1269.445043, 1270.469243, 1274.139643, 1274.924043, 1276.423843, 1281.449543, 1283.915543, 1284.392443, 1284.828943, 1287.5820429999999, 1288.2985429999999, 1290.2225429999999, 1290.778043, 1294.631843, 1296.719843, 1298.903343, 1301.526443, 1302.276343, 1302.490343, 1303.954143, 1309.437843, 1310.548043, 1314.348243, 1316.070543, 1316.260843, 1317.751243, 1317.935543, 1322.146643, 1328.689343, 1329.097243, 1329.546543, 1329.820643, 1333.801543, 1334.257043, 1338.509343, 1338.584243, 1339.321543, 1341.807143, 1345.492643, 1347.315443, 1348.668043, 1349.854643, 1352.199543, 1352.210943, 1356.886143, 1358.197243, 1361.030843, 1362.906043, 1364.2469429999999, 1366.268943, 1369.804343, 1374.347243, 1374.5440429999999, 1374.9235429999999, 1375.318443, 1377.875843, 1380.191043, 1381.156143, 1387.996743, 1388.038843, 1388.494543, 1390.815443, 1391.645243, 1397.819343, 1398.548343, 1399.623443, 1400.494943, 1402.532943, ]
B = [679.070505, 1358.141011]
答案 0 :(得分:4)
A
B
的每个元素(称为b
)b
在A
中适合的位置。b
大,一个小。b
。这能让你前进吗?
答案 1 :(得分:2)
使用NumPy数组和向量化操作:
import numpy as np
A = np.array(A)
B = np.array(B)
idx = np.abs(A - B[:, None]).argmin(1)
res = A[idx]
print(res)
array([ 762.859793, 1358.197243])
答案 2 :(得分:2)
您可以预处理长列表,以便计算相邻对之间的中间值。然后,您可以使用bisect来获取最接近的数字的索引:
from bisect import bisect
A = (762.859793, 763.544183, 764.272883, 766.190053, 767.108693, 767.123893, 768.933893, 770.619013, 771.375173, 771.522083, 774.9478730000001, 775.138843, 775.463863, 776.332693, 776.466303, 777.650083, 778.3751129999999, 781.922783, 782.592033, 784.224423, 784.779903, 785.2743429999999, 787.098133, 787.8258030000001, 787.8593030000001, 788.194393, 790.939383, 791.379753, 793.572803, 796.093073, 796.720693, 796.852473, 798.519643, 800.145803, 800.758723, 801.239613, 801.332453, 801.819333, 805.290823, 806.404353, 807.995663, 808.683403, 810.0645030000001, 810.695673, 810.7933929999999, 811.150203, 814.270803, 814.706783, 815.6527629999999, 819.127253, 819.820733, 821.030753, 821.166193, 823.111683, 824.637013, 826.707083, 826.997243, 827.441463, 828.529713, 830.364193, 831.241203, 831.2571929999999, 832.635243, 832.9749730000001, 834.222713, 837.932063, 838.250483, 838.695363, 840.0615730000001, 841.246143, 841.300043, 841.5062230000001, 844.7620730000001, 845.1879230000001, 846.261283, 847.139523, 849.0279429999999, 850.5646929999999, 850.726553, 851.8736529999999, 853.9559830000001, 854.171933, 857.674983, 858.035633, 858.327443, 858.9393230000001, 859.365103, 860.0646929999999, 861.035573, 861.496883, 864.127743, 866.558833, 867.0207429999999, 868.135673, 869.329093, 870.5860529999999, 871.1149230000001, 871.691343, 872.156483, 875.2681230000001, 875.921803, 876.6624029999999, 879.2358929999999, 879.7697029999999, 880.6190730000001, 882.563473, 883.1651529999999, 883.6522130000001, 884.311393, 886.585383, 888.0967929999999, 889.6549630000001, 890.564143, 891.0488230000001, 892.887933, 893.3944630000001, 896.817743, 897.9441429999999, 898.577573, 898.681813, 900.280573, 901.308643, 903.324453, 905.128653, 905.9143029999999, 906.190423, 906.641533, 907.232963, 909.896413, 912.3604630000001, 913.905633, 914.0172630000001, 915.186703, 915.764243, 917.266943, 921.000943, 921.958943, 923.0551429999999, 924.235543, 924.545743, 925.517043, 925.8717429999999, 927.906943, 931.119243, 931.856543, 933.8970429999999, 933.929043, 934.2806429999999, 935.171843, 936.956243, 938.8980429999999, 941.066643, 941.633743, 942.986043, 944.437243, 945.839343, 948.351143, 949.9056429999999, 950.118243, 950.587543, 951.3468429999999, 954.000243, 954.205843, 954.944843, 955.703343, 959.0683429999999, 959.298543, 960.0447429999999, 961.379543, 965.161943, 967.108743, 967.7327429999999, 968.199043, 968.438843, 969.973543, 970.540543, 971.3676429999999, 972.284543, 973.603743, 976.753143, 978.1982429999999, 978.214943, 978.545843, 979.070643, 980.912243, 982.989643, 985.355943, 986.1942429999999, 986.3504429999999, 986.4826429999999, 988.964343, 989.596543, 992.602043, 993.117943, 993.7612429999999, 994.3083429999999, 994.401343, 995.340643, 998.5397429999999, 999.000443, 1000.593343, 1000.6357429999999, 1001.2584429999999, 1001.334243, 1003.663543, 1004.3092429999999, 1006.184043, 1006.665143, 1007.890843, 1010.872443, 1011.4528429999999, 1012.454843, 1012.648543, 1012.825443, 1013.512643, 1015.107543, 1017.5950429999999, 1018.145843, 1022.399143, 1023.348243, 1023.4881429999999, 1023.703943, 1023.892743, 1024.563343, 1026.059343, 1028.420643, 1028.633143, 1028.928743, 1029.316843, 1032.945843, 1036.954843, 1037.770843, 1038.025643, 1039.704743, 1040.994643, 1041.791843, 1042.953943, 1043.592843, 1043.708343, 1050.5268429999999, 1052.079443, 1052.649543, 1052.684943, 1055.256443, 1060.528543, 1061.940143, 1062.041143, 1065.194343, 1065.722143, 1066.910543, 1068.947743, 1073.2487429999999, 1073.632343, 1074.822043, 1075.200543, 1076.141143, 1077.981343, 1079.280243, 1081.3022429999999, 1081.893343, 1085.848643, 1086.145043, 1087.4752429999999, 1087.7197429999999, 1089.479843, 1094.0820429999999, 1094.511343, 1094.685543, 1096.808443, 1097.674643, 1100.9245429999999, 1101.318843, 1101.795743, 1106.028343, 1106.752143, 1108.441043, 1109.063443, 1110.809843, 1112.244343, 1115.205043, 1115.793543, 1116.281043, 1117.533043, 1120.772743, 1120.943343, 1122.063243, 1126.348043, 1126.588843, 1127.782343, 1127.8493429999999, 1132.284543, 1133.968643, 1134.083643, 1134.812743, 1138.425743, 1140.277243, 1142.692943, 1143.479343, 1143.508343, 1146.1264429999999, 1147.5087429999999, 1148.0667429999999, 1149.901943, 1150.083443, 1154.467843, 1155.7469429999999, 1156.378443, 1157.213743, 1157.893943, 1161.299843, 1163.574343, 1165.368743, 1166.530043, 1167.6391429999999, 1168.554843, 1169.4887429999999, 1170.586043, 1175.934543, 1178.156243, 1178.610543, 1178.921943, 1179.145843, 1179.573643, 1187.012243, 1187.262843, 1187.406943, 1189.229243, 1191.636343, 1191.886343, 1194.272843, 1195.991843, 1199.937643, 1200.047843, 1201.449543, 1204.830543, 1205.389743, 1209.462843, 1210.561743, 1212.594143, 1213.386543, 1214.1418429999999, 1214.154443, 1219.073643, 1221.015143, 1222.636843, 1223.881443, 1227.9897429999999, 1228.923443, 1229.525743, 1233.922243, 1234.473743, 1235.452343, 1235.808943, 1240.449043, 1241.093643, 1245.171843, 1245.478243, 1246.061043, 1246.452043, 1249.927343, 1252.341943, 1255.264743, 1257.632643, 1258.441243, 1258.452643, 1259.841943, 1260.042343, 1265.0739429999999, 1266.0141429999999, 1267.6002429999999, 1269.445043, 1270.469243, 1274.139643, 1274.924043, 1276.423843, 1281.449543, 1283.915543, 1284.392443, 1284.828943, 1287.5820429999999, 1288.2985429999999, 1290.2225429999999, 1290.778043, 1294.631843, 1296.719843, 1298.903343, 1301.526443, 1302.276343, 1302.490343, 1303.954143, 1309.437843, 1310.548043, 1314.348243, 1316.070543, 1316.260843, 1317.751243, 1317.935543, 1322.146643, 1328.689343, 1329.097243, 1329.546543, 1329.820643, 1333.801543, 1334.257043, 1338.509343, 1338.584243, 1339.321543, 1341.807143, 1345.492643, 1347.315443, 1348.668043, 1349.854643, 1352.199543, 1352.210943, 1356.886143, 1358.197243, 1361.030843, 1362.906043, 1364.2469429999999, 1366.268943, 1369.804343, 1374.347243, 1374.5440429999999, 1374.9235429999999, 1375.318443, 1377.875843, 1380.191043, 1381.156143, 1387.996743, 1388.038843, 1388.494543, 1390.815443, 1391.645243, 1397.819343, 1398.548343, 1399.623443, 1400.494943, 1402.532943, )
B = (679.070505, 763, 1358.141011, 4000)
# A seems to be already sorted, otherwise we would have to sort it
# first
middles = [(A[i] + A[i+1])/2 for i in range(len(A)-1) ]
closest = [A[bisect(middles, value)] for value in B]
print(closest)
# [762.859793, 762.859793, 1358.197243, 1402.532943]
bisect(middles, value)
将返回您必须在value
列表中插入middles
的索引,这样它就位于两个中间值之间。这样,可以保证最接近这些中间点之间的值。
中线列表的创建是O(A的大小),以后每次使用bisect都会很快,因为它使用了二等分算法。
答案 3 :(得分:0)
为(difference, number)
中的每个元素制作成对的列表,B
。对于A
中的每个项目,计算与B
中的每个项目的差异,如果该差异小于存储的差异,请替换存储的数据。
A = (762.859793, 763.544183, 764.272883, 766.190053, 767.108693, 767.123893, 768.933893, 770.619013, 771.375173, 771.522083, 774.9478730000001, 775.138843, 775.463863, 776.332693, 776.466303, 777.650083, 778.3751129999999, 781.922783, 782.592033, 784.224423, 784.779903, 785.2743429999999, 787.098133, 787.8258030000001, 787.8593030000001, 788.194393, 790.939383, 791.379753, 793.572803, 796.093073, 796.720693, 796.852473, 798.519643, 800.145803, 800.758723, 801.239613, 801.332453, 801.819333, 805.290823, 806.404353, 807.995663, 808.683403, 810.0645030000001, 810.695673, 810.7933929999999, 811.150203, 814.270803, 814.706783, 815.6527629999999, 819.127253, 819.820733, 821.030753, 821.166193, 823.111683, 824.637013, 826.707083, 826.997243, 827.441463, 828.529713, 830.364193, 831.241203, 831.2571929999999, 832.635243, 832.9749730000001, 834.222713, 837.932063, 838.250483, 838.695363, 840.0615730000001, 841.246143, 841.300043, 841.5062230000001, 844.7620730000001, 845.1879230000001, 846.261283, 847.139523, 849.0279429999999, 850.5646929999999, 850.726553, 851.8736529999999, 853.9559830000001, 854.171933, 857.674983, 858.035633, 858.327443, 858.9393230000001, 859.365103, 860.0646929999999, 861.035573, 861.496883, 864.127743, 866.558833, 867.0207429999999, 868.135673, 869.329093, 870.5860529999999, 871.1149230000001, 871.691343, 872.156483, 875.2681230000001, 875.921803, 876.6624029999999, 879.2358929999999, 879.7697029999999, 880.6190730000001, 882.563473, 883.1651529999999, 883.6522130000001, 884.311393, 886.585383, 888.0967929999999, 889.6549630000001, 890.564143, 891.0488230000001, 892.887933, 893.3944630000001, 896.817743, 897.9441429999999, 898.577573, 898.681813, 900.280573, 901.308643, 903.324453, 905.128653, 905.9143029999999, 906.190423, 906.641533, 907.232963, 909.896413, 912.3604630000001, 913.905633, 914.0172630000001, 915.186703, 915.764243, 917.266943, 921.000943, 921.958943, 923.0551429999999, 924.235543, 924.545743, 925.517043, 925.8717429999999, 927.906943, 931.119243, 931.856543, 933.8970429999999, 933.929043, 934.2806429999999, 935.171843, 936.956243, 938.8980429999999, 941.066643, 941.633743, 942.986043, 944.437243, 945.839343, 948.351143, 949.9056429999999, 950.118243, 950.587543, 951.3468429999999, 954.000243, 954.205843, 954.944843, 955.703343, 959.0683429999999, 959.298543, 960.0447429999999, 961.379543, 965.161943, 967.108743, 967.7327429999999, 968.199043, 968.438843, 969.973543, 970.540543, 971.3676429999999, 972.284543, 973.603743, 976.753143, 978.1982429999999, 978.214943, 978.545843, 979.070643, 980.912243, 982.989643, 985.355943, 986.1942429999999, 986.3504429999999, 986.4826429999999, 988.964343, 989.596543, 992.602043, 993.117943, 993.7612429999999, 994.3083429999999, 994.401343, 995.340643, 998.5397429999999, 999.000443, 1000.593343, 1000.6357429999999, 1001.2584429999999, 1001.334243, 1003.663543, 1004.3092429999999, 1006.184043, 1006.665143, 1007.890843, 1010.872443, 1011.4528429999999, 1012.454843, 1012.648543, 1012.825443, 1013.512643, 1015.107543, 1017.5950429999999, 1018.145843, 1022.399143, 1023.348243, 1023.4881429999999, 1023.703943, 1023.892743, 1024.563343, 1026.059343, 1028.420643, 1028.633143, 1028.928743, 1029.316843, 1032.945843, 1036.954843, 1037.770843, 1038.025643, 1039.704743, 1040.994643, 1041.791843, 1042.953943, 1043.592843, 1043.708343, 1050.5268429999999, 1052.079443, 1052.649543, 1052.684943, 1055.256443, 1060.528543, 1061.940143, 1062.041143, 1065.194343, 1065.722143, 1066.910543, 1068.947743, 1073.2487429999999, 1073.632343, 1074.822043, 1075.200543, 1076.141143, 1077.981343, 1079.280243, 1081.3022429999999, 1081.893343, 1085.848643, 1086.145043, 1087.4752429999999, 1087.7197429999999, 1089.479843, 1094.0820429999999, 1094.511343, 1094.685543, 1096.808443, 1097.674643, 1100.9245429999999, 1101.318843, 1101.795743, 1106.028343, 1106.752143, 1108.441043, 1109.063443, 1110.809843, 1112.244343, 1115.205043, 1115.793543, 1116.281043, 1117.533043, 1120.772743, 1120.943343, 1122.063243, 1126.348043, 1126.588843, 1127.782343, 1127.8493429999999, 1132.284543, 1133.968643, 1134.083643, 1134.812743, 1138.425743, 1140.277243, 1142.692943, 1143.479343, 1143.508343, 1146.1264429999999, 1147.5087429999999, 1148.0667429999999, 1149.901943, 1150.083443, 1154.467843, 1155.7469429999999, 1156.378443, 1157.213743, 1157.893943, 1161.299843, 1163.574343, 1165.368743, 1166.530043, 1167.6391429999999, 1168.554843, 1169.4887429999999, 1170.586043, 1175.934543, 1178.156243, 1178.610543, 1178.921943, 1179.145843, 1179.573643, 1187.012243, 1187.262843, 1187.406943, 1189.229243, 1191.636343, 1191.886343, 1194.272843, 1195.991843, 1199.937643, 1200.047843, 1201.449543, 1204.830543, 1205.389743, 1209.462843, 1210.561743, 1212.594143, 1213.386543, 1214.1418429999999, 1214.154443, 1219.073643, 1221.015143, 1222.636843, 1223.881443, 1227.9897429999999, 1228.923443, 1229.525743, 1233.922243, 1234.473743, 1235.452343, 1235.808943, 1240.449043, 1241.093643, 1245.171843, 1245.478243, 1246.061043, 1246.452043, 1249.927343, 1252.341943, 1255.264743, 1257.632643, 1258.441243, 1258.452643, 1259.841943, 1260.042343, 1265.0739429999999, 1266.0141429999999, 1267.6002429999999, 1269.445043, 1270.469243, 1274.139643, 1274.924043, 1276.423843, 1281.449543, 1283.915543, 1284.392443, 1284.828943, 1287.5820429999999, 1288.2985429999999, 1290.2225429999999, 1290.778043, 1294.631843, 1296.719843, 1298.903343, 1301.526443, 1302.276343, 1302.490343, 1303.954143, 1309.437843, 1310.548043, 1314.348243, 1316.070543, 1316.260843, 1317.751243, 1317.935543, 1322.146643, 1328.689343, 1329.097243, 1329.546543, 1329.820643, 1333.801543, 1334.257043, 1338.509343, 1338.584243, 1339.321543, 1341.807143, 1345.492643, 1347.315443, 1348.668043, 1349.854643, 1352.199543, 1352.210943, 1356.886143, 1358.197243, 1361.030843, 1362.906043, 1364.2469429999999, 1366.268943, 1369.804343, 1374.347243, 1374.5440429999999, 1374.9235429999999, 1375.318443, 1377.875843, 1380.191043, 1381.156143, 1387.996743, 1388.038843, 1388.494543, 1390.815443, 1391.645243, 1397.819343, 1398.548343, 1399.623443, 1400.494943, 1402.532943, )
B = (679.070505, 1358.141011)
stored = [(float('inf'), None)]*len(B)
for a in A:
for i, ((curr_diff, _), b) in enumerate(zip(stored, B)):
diff = abs(a-b)
if diff < curr_diff:
stored[i] = (diff, a)
这给了我
[(83.78928799999994, 762.859793), (0.056232000000136395, 1358.197243)]