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


Python SortedDict.setdefault方法代码示例

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


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

示例1: compute_pagerank

# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import setdefault [as 别名]
def compute_pagerank(urls, inlinks, outlinks, b=.85, iters=20):
    """ Return a dictionary mapping each url to its PageRank.
    The formula is R(u) = (1/N)(1-b) + b * (sum_{w in B_u} R(w) / (|F_w|)

    Initialize all scores to 1.0.

    Params:
      urls.......SortedList of urls (names)
      inlinks....SortedDict mapping url to list of in links (backlinks)
      outlinks...Sorteddict mapping url to list of outlinks
    Returns:
      A SortedDict mapping url to its final PageRank value (float)

    >>> urls = SortedList(['a', 'b', 'c'])
    >>> inlinks = SortedDict({'a': ['c'], 'b': set(['a']), 'c': set(['a', 'b'])})
    >>> outlinks = SortedDict({'a': ['b', 'c'], 'b': set(['c']), 'c': set(['a'])})
    >>> sorted(compute_pagerank(urls, inlinks, outlinks, b=.5, iters=0).items())
    [('a', 1.0), ('b', 1.0), ('c', 1.0)]
    >>> iter1 = compute_pagerank(urls, inlinks, outlinks, b=.5, iters=1)
    >>> iter1['a']  # doctest:+ELLIPSIS
    0.6666...
    >>> iter1['b']  # doctest:+ELLIPSIS
    0.333...
    """
    ###TODO
    
    """
    R(u) = (1 - b)/N + b * sum( inlinks of u/outlink-number)
    
    """
        
    Ru = SortedDict()
    Rv = SortedDict()
    
    size = len(urls)
    
    # Initialize to 1.0 all URL's
    for k in urls:
        Ru.setdefault(k, 1.0)
    
    # Page Rank definition
    for i in range(iters):
        
        for url in urls:
        	
            try:
                Ru[url] = ((1-b)/size) + b * sum([Ru[x]/len(outlinks[x]) for x in inlinks[url] if len(outlinks[x])])
            except:
                pass
    
    return Ru
开发者ID:changediyasunny,项目名称:assignments,代码行数:53,代码来源:pagerank.py

示例2: read_links

# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import setdefault [as 别名]
def read_links(path):
    """
    Read the html pages in the data folder. Create and return two SortedDicts:
      inlinks: maps from a name to a SortedSet of names that link to it.
      outlinks: maps from a name to a SortedSet of names that it links to.
    For example:
    inlinks['Ada_Lovelace'] = SortedSet(['Charles_Babbage', 'David_Gelernter'], key=None, load=1000)
    outlinks['Ada_Lovelace'] = SortedSet(['Alan_Turing', 'Charles_Babbage'], key=None, load=1000)

    You should use the read_names and get_links function above.

    Params:
      path...the name of the data directory ('data')
    Returns:
      A (inlinks, outlinks) tuple, as defined above (i.e., two SortedDicts)
    """
    ###TODO
    
    inlinks = SortedDict()
    outlinks = SortedDict()
    
    names_dict = read_names(path)
    
    for page in names_dict:
    	
    	with open(path + os.sep + page, 'r') as fp:
    		html_page = fp.readlines()
    	
    	# Get links
    	temp_links = get_links(names_dict, str(html_page))
    	
    	# remove self link without checking, may save some time
    	try:
    		temp_links.remove(page)
    	except:
    		pass
    		    		
    	# Count outlinks
    	outlinks[page] = temp_links
    	
    	inlinks.setdefault(page,SortedSet())
    	# count inlinks from same single page-name
    	for link in outlinks[page]:
            inlinks.setdefault(link,SortedSet()).add(page)
    
    return inlinks, outlinks
开发者ID:changediyasunny,项目名称:assignments,代码行数:48,代码来源:pagerank.py

示例3: test_setdefault

# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import setdefault [as 别名]
def test_setdefault():
    mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
    temp = SortedDict(mapping)
    assert temp.setdefault('a', -1) == 0
    assert temp['a'] == 0
    assert temp.setdefault('A', -1) == -1
开发者ID:danbornside,项目名称:sorted_containers,代码行数:8,代码来源:test_coverage_sorteddict.py

示例4: collect_changed_files

# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import setdefault [as 别名]
 def collect_changed_files(self, ticket_frame: pd.DataFrame):
     changed_files = SortedDict()
     for i, files in enumerate(ticket_frame.ChangedFiles):
         for f in files:
             changed_files.setdefault(f, []).append(i)
     return changed_files
开发者ID:ashapochka,项目名称:saapy,代码行数:8,代码来源:analysis_support1.py

示例5: DotMap

# 需要导入模块: from sortedcontainers import SortedDict [as 别名]
# 或者: from sortedcontainers.SortedDict import setdefault [as 别名]

#.........这里部分代码省略.........
            d[k] = v
        return d

    def pprint(self):
        pprint(self.to_dict())

    # proper dict subclassing
    def values(self):
        return self._map.values()

    @staticmethod
    def parse_other(other):
        if type(other) is DotMap:
            return other._map
        else:
            return other

    def __cmp__(self, other):
        other = DotMap.parse_other(other)
        return self._map.__cmp__(other)

    def __eq__(self, other):
        other = DotMap.parse_other(other)
        if not isinstance(other, dict):
            return False
        return self._map.__eq__(other)

    def __ge__(self, other):
        other = DotMap.parse_other(other)
        return self._map.__ge__(other)

    def __gt__(self, other):
        other = DotMap.parse_other(other)
        return self._map.__gt__(other)

    def __le__(self, other):
        other = DotMap.parseOther(other)
        return self._map.__le__(other)

    def __lt__(self, other):
        other = DotMap.parse_other(other)
        return self._map.__lt__(other)

    def __ne__(self, other):
        other = DotMap.parse_other(other)
        return self._map.__ne__(other)

    def __delitem__(self, key):
        return self._map.__delitem__(key)

    def __len__(self):
        return self._map.__len__()

    def copy(self):
        return self

    def get(self, key, default=None):
        return self._map.get(key, default)

    def has_key(self, key):
        return key in self._map

    def iterkeys(self):
        return self._map.iterkeys()

    def itervalues(self):
        return self._map.itervalues()

    def keys(self):
        return self._map.keys()

    def pop(self, key, default=None):
        return self._map.pop(key, default)

    def setdefault(self, key, default=None):
        return self._map.setdefault(key, default)

    def viewitems(self):
        if version_info.major == 2 and version_info.minor >= 7:
            return self._map.viewitems()
        else:
            return self._map.items()

    def viewkeys(self):
        if version_info.major == 2 and version_info.minor >= 7:
            return self._map.viewkeys()
        else:
            return self._map.keys()

    def viewvalues(self):
        if version_info.major == 2 and version_info.minor >= 7:
            return self._map.viewvalues()
        else:
            return self._map.values()

    @classmethod
    def fromkeys(cls, seq, value=None):
        d = DotMap()
        d._map = SortedDict.fromkeys(seq, value)
        return d
开发者ID:mmarkov,项目名称:dotmap,代码行数:104,代码来源:__init__.py


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