当前位置: 首页>>代码示例>>Python>>正文


Python SortedList.add方法代码示例

本文整理汇总了Python中sortedcontainers.SortedList.add方法的典型用法代码示例。如果您正苦于以下问题:Python SortedList.add方法的具体用法?Python SortedList.add怎么用?Python SortedList.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sortedcontainers.SortedList的用法示例。


在下文中一共展示了SortedList.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: predict

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
    def predict(self, X):
        y = np.zeros(len(X))
        for i,x in enumerate(X): # test points
            sl = SortedList(load=self.k) # stores (distance, class) tuples
            for j,xt in enumerate(self.X): # training points
                diff = x - xt
                d = diff.dot(diff)
                if len(sl) < self.k:
                    # don't need to check, just add
                    sl.add( (d, self.y[j]) )
                else:
                    if d < sl[-1][0]:
                        del sl[-1]
                        sl.add( (d, self.y[j]) )
            # print "input:", x
            # print "sl:", sl

            # vote
            votes = {}
            for _, v in sl:
                # print "v:", v
                votes[v] = votes.get(v,0) + 1
            # print "votes:", votes, "true:", Ytest[i]
            max_votes = 0
            max_votes_class = -1
            for v,count in votes.iteritems():
                if count > max_votes:
                    max_votes = count
                    max_votes_class = v
            y[i] = max_votes_class
        return y
开发者ID:vivianduan,项目名称:machine_learning_examples,代码行数:33,代码来源:knn.py

示例2: test_copy

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
def test_copy():
    alpha = SortedList(range(100))
    alpha._reset(7)
    beta = alpha.copy()
    alpha.add(100)
    assert len(alpha) == 101
    assert len(beta) == 100
开发者ID:grantjenks,项目名称:sorted_containers,代码行数:9,代码来源:test_coverage_sortedlist.py

示例3: test_copy_copy

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
def test_copy_copy():
    import copy
    alpha = SortedList(range(100), load=7)
    beta = copy.copy(alpha)
    alpha.add(100)
    assert len(alpha) == 101
    assert len(beta) == 100
开发者ID:adamchainz,项目名称:sorted_containers,代码行数:9,代码来源:test_coverage_sortedlist.py

示例4: extract_collocations

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
    def extract_collocations(self, metric_class):
        assert issubclass(metric_class, Metric)
        metric = metric_class()
        collocations = SortedList(key=lambda x: -x[0])
        
        unigram_counts = self.language_model.get_unigrams()
        bigram_counts = self.language_model.get_bigrams()

        for (first, last), freq_bigram in bigram_counts.items():

            if self.exclude_punctuation:
                if first in self.PUNCT or last in self.PUNCT:
                    continue

            if self.exclude_conj:
                if first in self.CONJ_RU or last in self.CONJ_RU:
                    continue

            if self.exclude_props:
                if first in self.PROPOSITIONS_RU or last in self.PROPOSITIONS_RU:
                    continue

            freq_first, freq_last = unigram_counts[first], unigram_counts[last]
            
            metric_val = metric.evaluate(freq_first, freq_last, freq_bigram,
                                         self.language_model.get_vocab_size())
            collocations.add((metric_val, freq_first,
                              freq_last, freq_bigram,
                              first, last))
            
        return collocations
开发者ID:roddar92,项目名称:linguistics_problems,代码行数:33,代码来源:Collocations.py

示例5: score_of_a_vacated_people

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
 def score_of_a_vacated_people(self, universo, work='translations'):
     factor = math.sqrt(len(universo))
     scores = SortedList(load=round(factor))
     for (people, score) in self.__scores__().items():
         if people in universo:
             scores.add(TranslatorScore(people, score[work]))
     return scores.pop(0)
开发者ID:OSMBrasil,项目名称:paicemana,代码行数:9,代码来源:mdanalyzer.py

示例6: Episode_scores

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
class Episode_scores(object):
  def __init__(self, options):
    self.maxlen = options.score_averaging_length
    self.threshold = options.score_highest_ratio
    self.episode_scores = deque()
    self.episode_scores.append(0) # to avoid 0-div in first averaging
    self.episode_scores_sum = 0
    self.sorted_scores = SortedList()
    self.sorted_scores.add(0) # align to episode_scores
    self.num_episode = 0
    self.options = options

  def add(self, n, global_t, thread_index):
    self.episode_scores_sum += n
    self.episode_scores.append(n)
    self.sorted_scores.add(-n) # trick to use SortedList in reverse order
    if len(self.episode_scores) > self.maxlen:
      oldest = self.episode_scores.popleft()
      self.sorted_scores.remove(-oldest)
      self.episode_scores_sum -= oldest
    self.num_episode += 1
    if self.num_episode % self.options.average_score_log_interval == 0:
      print("@@@ Average Episode score = {:.6f}, s={:9d},th={}".format(self.average(), global_t, thread_index))

  def average(self):
    return self.episode_scores_sum / len(self.episode_scores)

  def is_highscore(self, n):
    sorted_scores = self.sorted_scores
    num_scores = len(sorted_scores)
    sorted_scores.add(-n)
    index = sorted_scores.index(-n)
    highest_ratio = (index + 1) / num_scores
    sorted_scores.remove(-n)
    return highest_ratio <= self.threshold
开发者ID:Itsukara,项目名称:async_deep_reinforce,代码行数:37,代码来源:a3c_training_thread.py

示例7: iana_rir_gen_ip_list

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
def iana_rir_gen_ip_list(user_rir_list):

    # generates a list of networks that can be blocked by RIR

    # we use a SortedList so that elements are inserted in order. This allows cidr_merge to work
    rir_slash_eight_list = SortedList()

    with open('iana') as iana_file:

        iana_csv = csv.reader(iana_file)

        for line in iana_csv:

            for rir in user_rir_list:

                # case in which the whois line from our csv contains the RIR
                if rir in line[3]:

                    network = line[0].lstrip('0')
                    rir_slash_eight_list.add(netaddr.IPNetwork(network))

                    # if we find a match, there is no reason to see if the other RIRs are on the same line
                    break

        # run cidr_merge to summarize
        rir_slash_eight_list = netaddr.cidr_merge(rir_slash_eight_list)

    return rir_slash_eight_list
开发者ID:bjames,项目名称:geoblock,代码行数:30,代码来源:geoblock.py

示例8: find_latest

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
 def find_latest(self):
     sorted = SortedList()
     for i in self.bucket.list(prefix=self.db_name):
         parts = i.name.split('/')
         if len(parts) == 3:
             d = datetime.datetime.strptime(parts[1], "%m%d%Y").date()
             sorted.add(d)
     return sorted[len(sorted)-1].strftime('%m%d%Y')
开发者ID:jglazner,项目名称:s3tools,代码行数:10,代码来源:restore.py

示例9: read_rirs

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
def read_rirs(country_list, permit, rir_list=RIR_NAMES):

    # list containing our file objects
    file_list = []

    # we use a SortedList so that elements are inserted in order. This allows cidr_merge to work
    rir_ips = SortedList()

    # Open the files we downloaded earlier and store the file object
    for rir in rir_list:
        file_list.append(open(rir))

    for f in file_list:

        for line in f:

            curr_line = line.split('|')

            try:

                # we want only the ipv4 lines that are for a specific country
                # also only want countries that we are going to block
                if (curr_line[2] == "ipv4" and curr_line[1] != "*") and \
                    ((permit and curr_line[1] not in country_list) or
                     (not permit and curr_line[1] in country_list)):

                    country_code = curr_line[1]
                    network_id = curr_line[3]
                    wildcard = int(curr_line[4])-1

                    try:

                        # Add network to list, if the number of IPs was not a
                        # power of 2 (wildcard is not valid).
                        # AddrFormatError is thrown
                        rir_ips.add(netaddr.IPNetwork(network_id + "/" + str(netaddr.IPAddress(wildcard))))

                    # Handle case in where our mask is invalid by rounding DOWN
                    except netaddr.AddrFormatError:

                        print "rounded network " + network_id + " with " + str(wildcard) + \
                              " hosts up to nearest power of 2"
                        wildcard = next_power_of_2(wildcard) - 1
                        print wildcard + 1
                        rir_ips.add(netaddr.IPNetwork(network_id + "/" + str(netaddr.IPAddress(wildcard))))

            # IndexErrors only occur when parsing columns we don't need
            except IndexError:

                pass

        f.close()

    # cidr_merge takes our list of IPs and summarizes subnets where possible
    # this greatly decreases the number of ACL entries
    rir_ips = netaddr.cidr_merge(rir_ips)

    return rir_ips
开发者ID:bjames,项目名称:geoblock,代码行数:60,代码来源:geoblock.py

示例10: InMemoryBackend

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
class InMemoryBackend(object):
    """
    The backend that keeps the results in the memory.
    """
    def __init__(self, *args, **kwargs):
        def get_timestamp(result):
            return timestamp_parser.parse(result['timestamp'])

        self._results = dict()
        self._sorted = SortedList(key=get_timestamp)

    def disconnect(self):
        return succeed(None)

    def store(self, result):
        """
        Store a single benchmarking result and return its identifier.

        :param dict result: The result in the JSON compatible format.
        :return: A Deferred that produces an identifier for the stored
            result.
        """
        id = uuid4().hex
        self._results[id] = result
        self._sorted.add(result)
        return succeed(id)

    def retrieve(self, id):
        """
        Retrive a result by the given identifier.
        """
        try:
            return succeed(self._results[id])
        except KeyError:
            return fail(ResultNotFound(id))

    def query(self, filter, limit=None):
        """
        Return matching results.
        """
        matching = []
        for result in reversed(self._sorted):
            if len(matching) == limit:
                break
            if filter.viewitems() <= result.viewitems():
                matching.append(result)
        return succeed(matching)

    def delete(self, id):
        """
        Delete a result by the given identifier.
        """
        try:
            result = self._results.pop(id)
            self._sorted.remove(result)
            return succeed(None)
        except KeyError:
            return fail(ResultNotFound(id))
开发者ID:carriercomm,项目名称:benchmark-server,代码行数:60,代码来源:httpapi.py

示例11: dir

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
 def dir(self, file_pattern):
     attrs = self.sftp.listdir_attr(self.remote_dir)
     filtered = SortedList()
     for attr in attrs:
         if hasattr(attr, "filename"):
             filename = attr.filename
             if re.match(file_pattern, filename):
                 remote_file = RemoteFile(filename, attr.st_mtime)
                 filtered.add(remote_file)
     return filtered
开发者ID:tjtaill,项目名称:Scripts,代码行数:12,代码来源:bw_sftp_session.py

示例12: test_count

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
def test_count():
    slt = SortedList(load=7)

    assert slt.count(0) == 0

    for iii in range(100):
        for jjj in range(iii):
            slt.add(iii)
        slt._check()

    for iii in range(100):
        assert slt.count(iii) == iii
开发者ID:sbagri,项目名称:sorted_containers,代码行数:14,代码来源:test_coverage_sortedlist.py

示例13: arrayRDP

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
def arrayRDP(arr, epsilon=0.0, n=None):
    """
    This is a slightly modified version of the _aRDP function, that accepts
    as arguments the tolerance in the distance and the maximum number of points
    the algorithm can select.
    **Note:** The results of this algoritm should be identical to the arrayRDP
    function if the *n* parameter is not specified. In that case, the
    performance is slightly worse, although the asymptotic complexity is the
    same. For this reason, this function internally delegates the solution in
    that function if the *n* parameter is missing.

    Parameters
    ----------
    arr:
        Array of values of consecutive points.
    epsilon:
        Maximum difference allowed in the simplification process.
    n:
        Maximum number of points of the resulted simplificated array.

    Returns
    -------
    out:
        Array of indices of the selected points.
    """
    if n is None:
        return _aRDP(arr, epsilon)
    if epsilon <= 0.0:
        raise ValueError('Epsilon must be > 0.0')
    n = n or len(arr)
    if n < 3:
        return arr
    fragments = SortedDict()
    #We store the distances as negative values due to the default order of
    #sorteddict
    dist, idx = max_vdist(arr, 0, len(arr) - 1)
    fragments[(-dist, idx)] = (0, len(arr) - 1)
    while len(fragments) < n-1:
        (dist, idx), (first, last) = fragments.popitem(last=False)
        if -dist <= epsilon:
            #We have to put again the last item to prevent loss
            fragments[(dist, idx)] = (first, last)
            break
        else:
            #We have to break the fragment in the selected index
            dist, newidx = max_vdist(arr, first, idx)
            fragments[(-dist, newidx)] = (first, idx)
            dist, newidx = max_vdist(arr, idx, last)
            fragments[(-dist, newidx)] = (idx, last)
    #Now we have to get all the indices in the keys of the fragments in order.
    result = SortedList(i[0] for i in fragments.itervalues())
    result.add(len(arr) - 1)
    return np.array(result)
开发者ID:citiususc,项目名称:qrsdel,代码行数:55,代码来源:rame_douglas_peucker.py

示例14: test_getitem

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
def test_getitem():
    random.seed(0)
    slt = SortedList(load=17)

    lst = list()

    for rpt in range(100):
        val = random.random()
        slt.add(val)
        lst.append(val)

    lst.sort()

    assert all(slt[idx] == lst[idx] for idx in range(100))
    assert all(slt[idx - 99] == lst[idx - 99] for idx in range(100))
开发者ID:sbagri,项目名称:sorted_containers,代码行数:17,代码来源:test_coverage_sortedlist.py

示例15: get_3_most_ambiguous

# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import add [as 别名]
 def get_3_most_ambiguous(self, X, Y):
   P = self.predict_proba(X)
   N = len(X)
   sl = SortedList(load=3) # stores (distance, sample index) tuples
   for n in xrange(N):
     p = P[n]
     dist = np.abs(p - 0.5)
     if len(sl) < 3:
       sl.add( (dist, n) )
     else:
       if dist < sl[-1][0]:
         del sl[-1]
         sl.add( (dist, n) )
   indexes = [v for k, v in sl]
   return X[indexes], Y[indexes]
开发者ID:ShuvenduBikash,项目名称:machine_learning_examples,代码行数:17,代码来源:nb.py


注:本文中的sortedcontainers.SortedList.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。