为了账号安全,请及时绑定邮箱和手机立即绑定

如何“剪掉”系列的尾巴

如何“剪掉”系列的尾巴

心有法竹 2021-03-20 15:08:33
我列出了10个单词的分数。第一个x往往比其余的重要得多。所以我想找到x。例如,绘制此列表显示第三学期之后的平稳期。因此,我们保留前三个学期。甚至从直觉上讲,保持前三个词似乎是适当的。    badge                                   =>    7.00709342956543    unlocked                                =>    7.00709342956543    foursquare                              =>   5.830315748850505    https                                   =>   5.001254081726074    you've unlocked                         =>   4.954763253529866    50xxxxxx badge                          =>   4.954763253529866    all badges                              =>   4.954763253529866    unlocked far                            =>   4.954763253529866    badges                                  =>   4.954763253529866    just unlocked                           =>   4.954763253529866但是,如何以编程方式生成此临界值呢?我更喜欢标准库中可用的内容。
查看完整描述

2 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

我假设您的“术语”将按照您提供的示例所示的降序排列。我将简单地建立一个增量(例如0.5),该增量表示一个足以被忽略的差异。


然后,我将遍历术语集合,将它们添加到结果集合中,并且一旦我看到我先前看到的术语的“增量”内的一个术语,我将结束迭代,并有可能将最后一次看到的术语从结果集合中删除,如下所示:好吧。


那有意义吗?


看起来像这样:


delta = 0.5

result = []

for term in termMapSortedKeys:

     if (previousTermValue - delta >= termMap[term]):

          break

     else:

          result.append(term)

          previousTermValue = termMap[term]

del result[-1]

return result


查看完整回答
反对 回复 2021-03-30
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

对于std-lib友好的方法,可以使用itertools.tee在列表中的项目之间进行比较,并以迭代器的形式返回增量。然后使用itertools.takewhile在您的公差范围内获取数据。


import itertools, sys


def delta(data):

    '''yield the original data and the delta to the next item as tuple'''

    a, b = itertools.tee(data)

    yeild (next(b, None), sys.maxint) # assume the first item always passes :)

    for n in itertools.izip(a, b):

        yield n[1], abs(n[1] - n[0])



# example...

data = [0,1,2,3,4,6,6.125,6.25,6.375,6,6,6.25,5,6,6, 4.5, 2.5, 7]

data.sort()       

print data

# [0, 1, 2, 2.5, 3, 4, 4.5, 5, 6, 6, 6, 6, 6, 6.125, 6.25, 6.25, 6.375, 7]



filter_fn = lambda x: x[1] > .05 # tolerance goes here...

trimmed = [item[0] for item in itertools.takewhile(filter_fn , delta(data))]

print trimmed 

# [0, 1, 2, 2.5, 3, 4, 4.5, 5, 6]


查看完整回答
反对 回复 2021-03-30
  • 2 回答
  • 0 关注
  • 157 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号