本文整理汇总了Python中sets.Set方法的典型用法代码示例。如果您正苦于以下问题:Python sets.Set方法的具体用法?Python sets.Set怎么用?Python sets.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sets
的用法示例。
在下文中一共展示了sets.Set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: precision
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def precision(reference, test):
"""
Given a set of reference values and a set of test values, return
the percentage of test values that appear in the reference set.
In particular, return |C{reference}S{cap}C{test}|/|C{test}|.
If C{test} is empty, then return C{None}.
@type reference: C{Set}
@param reference: A set of reference values.
@type test: C{Set}
@param test: A set of values to compare against the reference set.
@rtype: C{float} or C{None}
"""
if len(test) == 0:
return None
else:
return float(len(reference.intersection(test)))/len(test)
示例2: recall
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def recall(reference, test):
"""
Given a set of reference values and a set of test values, return
the percentage of reference values that appear in the test set.
In particular, return |C{reference}S{cap}C{test}|/|C{reference}|.
If C{reference} is empty, then return C{None}.
@type reference: C{Set}
@param reference: A set of reference values.
@type test: C{Set}
@param test: A set of values to compare against the reference set.
@rtype: C{float} or C{None}
"""
if len(reference) == 0:
return None
else:
return float(len(reference.intersection(test)))/len(reference)
示例3: demo
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def demo():
print '-'*75
reference = 'DET NN VB DET JJ NN NN IN DET NN'.split()
test = 'DET VB VB DET NN NN NN IN DET NN'.split()
print 'Reference =', reference
print 'Test =', test
print 'Confusion matrix:'
print ConfusionMatrix(reference, test)
print 'Accuracy:', accuracy(reference, test)
print '-'*75
reference_set = sets.Set(reference)
test_set = sets.Set(test)
print 'Reference =', reference_set
print 'Test = ', test_set
print 'Precision:', precision(reference_set, test_set)
print ' Recall:', recall(reference_set, test_set)
print 'F-Measure:', f_measure(reference_set, test_set)
print '-'*75
示例4: decorator_factory
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def decorator_factory(cls):
"""
Take a class with a ``.caller`` method and return a callable decorator
object. It works by adding a suitable __call__ method to the class;
it raises a TypeError if the class already has a nontrivial __call__
method.
"""
attrs = set(dir(cls))
if '__call__' in attrs:
raise TypeError('You cannot decorate a class with a nontrivial '
'__call__ method')
if 'call' not in attrs:
raise TypeError('You cannot decorate a class without a '
'.call method')
cls.__call__ = __call__
return cls
示例5: add_parser
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def add_parser(self, name, **kwargs):
# set prog from the existing prefix
if kwargs.get('prog') is None:
kwargs['prog'] = '%s %s' % (self._prog_prefix, name)
aliases = kwargs.pop('aliases', ())
# create a pseudo-action to hold the choice help
if 'help' in kwargs:
help = kwargs.pop('help')
choice_action = self._ChoicesPseudoAction(name, aliases, help)
self._choices_actions.append(choice_action)
# create the parser and add it to the map
parser = self._parser_class(**kwargs)
self._name_parser_map[name] = parser
# make parser available under aliases also
for alias in aliases:
self._name_parser_map[alias] = parser
return parser
示例6: _add_action
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def _add_action(self, action):
# resolve any conflicts
self._check_conflict(action)
# add to actions list
self._actions.append(action)
action.container = self
# index the action by any option strings it has
for option_string in action.option_strings:
self._option_string_actions[option_string] = action
# set the flag if any option strings look like negative numbers
for option_string in action.option_strings:
if self._negative_number_matcher.match(option_string):
if not self._has_negative_number_optionals:
self._has_negative_number_optionals.append(True)
# return the created action
return action
示例7: _header_check
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def _header_check(self, headerslist):
"""
Checks to see if any of the headers are missing, raises error if so.
Also informs the user of redundent headers in the input file.
"""
# Check which of the pre-defined headers are missing
headers = Set((getline(self.filename, 1).rstrip("\n")).split(","))
missing_headers = headerslist.difference(headers)
if len(missing_headers) > 0:
output_string = ", ".join([value for value in missing_headers])
raise IOError("The following headers are missing from the input "
"file: %s" % output_string)
additional_headers = headers.difference(headerslist)
if len(additional_headers) > 0:
for header in additional_headers:
print("Header %s not recognised - ignoring this data!" %
header)
return
示例8: _header_check
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def _header_check(self):
"""
Checks to see if any of the headers are missing, raises error if so.
Also informs the user of redundent headers in the input file.
"""
# Check which of the pre-defined headers are missing
headers = Set((getline(self.filename, 1).rstrip("\n")).split(","))
missing_headers = HEADER_LIST.difference(headers)
if len(missing_headers) > 0:
output_string = ", ".join([value for value in missing_headers])
raise IOError("The following headers are missing from the input "
"file: %s" % output_string)
additional_headers = headers.difference(HEADER_LIST)
if len(additional_headers) > 0:
for header in additional_headers:
print("Header %s not recognised - ignoring this data!" %
header)
return
示例9: render
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def render(self, context):
key = self.queryset_var.var
value = self.queryset_var.resolve(context)
if isinstance(self.paginate_by, int):
paginate_by = self.paginate_by
else:
paginate_by = self.paginate_by.resolve(context)
paginator = Paginator(value, paginate_by, self.orphans)
try:
page_obj = paginator.page(context['request'].page)
except InvalidPage:
if INVALID_PAGE_RAISES_404:
raise Http404('Invalid page requested. If DEBUG were set to ' +
'False, an HTTP 404 page would have been shown instead.')
context[key] = []
context['invalid_page'] = True
return ''
if self.context_var is not None:
context[self.context_var] = page_obj.object_list
else:
context[key] = page_obj.object_list
context['paginator'] = paginator
context['page_obj'] = page_obj
return ''
示例10: endData
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def endData(self, containerClass=NavigableString):
if self.currentData:
currentData = u''.join(self.currentData)
if (currentData.translate(self.STRIP_ASCII_SPACES) == '' and
not set([tag.name for tag in self.tagStack]).intersection(
self.PRESERVE_WHITESPACE_TAGS)):
if '\n' in currentData:
currentData = '\n'
else:
currentData = ' '
self.currentData = []
if self.parseOnlyThese and len(self.tagStack) <= 1 and \
(not self.parseOnlyThese.text or \
not self.parseOnlyThese.search(currentData)):
return
o = containerClass(currentData)
o.setup(self.currentTag, self.previous)
if self.previous:
self.previous.next = o
self.previous = o
self.currentTag.contents.append(o)
示例11: _add_library
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def _add_library(self, name, sources, install_dir, build_info):
"""Common implementation for add_library and add_installed_library. Do
not use directly"""
build_info = copy.copy(build_info)
name = name #+ '__OF__' + self.name
build_info['sources'] = sources
# Sometimes, depends is not set up to an empty list by default, and if
# depends is not given to add_library, distutils barfs (#1134)
if not 'depends' in build_info:
build_info['depends'] = []
self._fix_paths_dict(build_info)
# Add to libraries list so that it is build with build_clib
self.libraries.append((name, build_info))
示例12: f_measure
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def f_measure(reference, test, alpha=0.5):
"""
Given a set of reference values and a set of test values, return
the f-measure of the test values, when compared against the
reference values. The f-measure is the harmonic mean of the
L{precision} and L{recall}, weighted by C{alpha}. In particular,
given the precision M{p} and recall M{r} defined by:
- M{p} = |C{reference}S{cap}C{test}|/|C{test}|
- M{r} = |C{reference}S{cap}C{test}|/|C{reference}|
The f-measure is:
- 1/(C{alpha}/M{p} + (1-C{alpha})/M{r})
If either C{reference} or C{test} is empty, then C{f_measure}
returns C{None}.
@type reference: C{Set}
@param reference: A set of reference values.
@type test: C{Set}
@param test: A set of values to compare against the reference set.
@rtype: C{float} or C{None}
"""
p = precision(reference, test)
r = recall(reference, test)
if p is None or r is None:
return None
if p == 0 or r == 0:
return 0
return 1.0/(alpha/p + (1-alpha)/r)
示例13: edits1
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def edits1(word):
n = len(word)
return set([word[0:i]+word[i+1:] for i in range(n)] + # deletion
[word[0:i]+word[i+1]+word[i]+word[i+2:] for i in range(n-1)] + # transposition
[word[0:i]+c+word[i+1:] for i in range(n) for c in alphabet] + # alteration
[word[0:i]+c+word[i:] for i in range(n+1) for c in alphabet]) # insertion
示例14: known
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def known(words):
#return set(w for w in words if w in NWORDS)
s = set()
for w in words:
if w in NWORDS: s.add(w)
return s
示例15: modifies_known_mutable
# 需要导入模块: import sets [as 别名]
# 或者: from sets import Set [as 别名]
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False