[英].bisect not finding closest value in dictionary
I have created a list: 我创建了一个列表:
arraynums = [ 0.3888553 0.3898553 0.3908553 0.3918553 0.3928553 0.3938553
0.3948553 0.3958553 0.3968553 0.3978553 0.3988553]
and a dictionary that has been sorted by key values (here's a portion of the dictionary): 以及已按键值排序的字典(这是字典的一部分):
sd =({'0.3880434': ['GGATCG'], '0.3883449': ['TTCACG'], '0.388449': ['ATGGCG'], '0.3890966': ['ACTCGC'], '0.3893325': ['GTGGAT'], '0.3893478': ['GATACG'], '0.3900749': ['CAGAAG'], '0.3900875': ['CGAGAG'], '0.3900915': ['ATCGGG'], '0.3901032': ['CACCGG'], '0.3901743': ['AAAGAC'], '0.3906361': ['TACGGC'], '0.390682': ['CCATCG'], '0.3909258': ['GGATGA'], '0.3910728': ['AAGATA'], '0.391648': ['GCAACG'], '0.3919125': ['AGGACT', 'GATCGC'], '0.3921844': ['AGAGAA'], '0.3922956': ['CGGGAA'], '0.3927617': ['ATGGAA'], '0.3927763': ['TTGTCG'], '0.3928683': ['ACAGAC'], '0.39309': ['CGCGCT'], '0.3938553': ['AGGACG'], '0.3940998': ['AAGAGC'], '0.3941768': ['GTCGGA'], '0.394966': ['CGTTCC'], '0.395116': ['TGGAAG'], '0.3954179': ['CCGTCC'], '0.3955623': ['AATCGC'], '0.3956923': ['GGACGG']})
I have been using this code to find the closest values to the values listed in the above list: 我一直在使用此代码来查找与以上列表中列出的值最接近的值:
for k in arraynums:
index = sd.bisect(k)
key = sd.iloc[index]
seq = sd[key]
However, the key and seq's printed from this portion of the code does not correctly identify the closest values for k. 但是,从此部分代码中打印出来的键和seq不能正确识别k的最接近值。 I'm not quite sure what is going wrong.
我不太确定出了什么问题。 I think it might have to do with the way I created the
arraynums
list. 我认为这可能与我创建
arraynums
列表的方式有关。 I created the list using this: 我使用以下命令创建了列表:
arraynums = numpy.arange(float(middlevalue) - 0.005, float(middlevalue) + 0.005, 0.001)
EDIT: A note to the dictionary above: some of the values are negative and the output for each key is the same negative value... I've also sorted the dictionary using SortedDict()
编辑:上面的字典的注释:一些值是负数,每个键的输出是相同的负值...我也使用
SortedDict()
对字典进行了排序
beyond the type mismatch ( sd
keys are str and arraynums
elements are float), an efficient way can be: 除了类型不匹配(
sd
键是str而arraynums
元素是float)之外,一种有效的方法可以是:
keys=list(zip(sd.items()))
values= array([x[0] for x in sd.values()])
indices=np.searchsorted(sorted(sd.keys()),arraynums)
In [390]: indices
Out[390]: array([ 3, 6, 13, 16, 21, 23, 26, 31, 31, 31, 31], dtype=int64)
indices
says that arraynums[0] is beetween keys[2] and keys[3] and so on (see searchsorted ). indices
表示arraynums [0]在keys [2]和keys [3]之间,依此类推(请参阅searchsorted )。 There is just a problem with last values : it can be avoid by an other choice of bounds. 最后一个值只是一个问题:可以通过其他边界选择来避免。 You have just now to compare for the closest and conclude.
您现在需要比较以得出最接近的结论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.