在python中枚举(enumerate in python)
说,
term='asdf'; InvertedIndex = {}; InvertedIndex[term] = [1,2,2,2,4,5,6,6,6,6,7] term='asdf'; InvertedIndex = {}; InvertedIndex[term] = [1,2,2,2,4,5,6,6,6,6,7] 。
现在我们有了这个没有的功能。 任何项目的出现。 这是我遇到问题的功能。
def TF(term, doc):
idx = InvertedIndex[term].index(doc)
return next(i for i, item in enumerate(InvertedIndex[term][idx:])
if item != doc)
TF(term, 1)给1, TF(term, 1)给1, TF(term, 1)给1 TF(term, 4) 。 好到目前为止。
但它给TF(term, 7) StopIteration错误TF(term, 7) 。 如果我有InvertedIndex[term] = [7]并且称为TF(term, 7)它也会给出相同的错误。 怎么解决?
编辑:关于功能目标的澄清。 该功能应该算不了。 一件物品的出现。 考虑到使用的示例TF(term,2)必须返回3,因为它在InvertedIndex [term]中出现了3次
解:
def TF(term, doc):
return InvertedIndex[term].count(doc)
Say,
term='asdf'; InvertedIndex = {}; InvertedIndex[term] = [1,2,2,2,4,5,6,6,6,6,7].
Now we have this function which counts no. of occurances of any item. This is the function I've having a problem with.
def TF(term, doc):
idx = InvertedIndex[term].index(doc)
return next(i for i, item in enumerate(InvertedIndex[term][idx:])
if item != doc)
It is giving 1 for TF(term, 1), 3 for TF(term, 2),1 for TF(term, 4). Fine so far.
But it is giving StopIteration error for TF(term, 7). It is also giving same error if I had InvertedIndex[term] = [7] and called TF(term, 7). How to fix it?
Edit: Clarification about aim of the function. that function is supposed to count no. of occurances of an item. Considering the used example TF(term, 2) must return 3 because it occured 3 times in InvertedIndex[term]
Solution:
def TF(term, doc):
return InvertedIndex[term].count(doc)
原文:https://stackoverflow.com/questions/4062448
2020-10-10 10:10
满意答案
我觉得我在另一个答案上写了那个循环,但你想要做的正确答案是InvertedIndex[term].count(doc)
这将计算doc在列表中出现的次数。
I feel like I wrote that loop on another answer but the correct answer for what you want to do is InvertedIndex[term].count(doc)
This will count the number of times that doc occurs in the list.
2010-10-31
相关问答
enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举、列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值 enumerate多用于在for循环中得到计数 例如对于一个seq,得到: (0, seq[0]), (1, seq[1]), (2, seq[2]) 1 1 enumerate()返回的是一个enumerate对象,例如: enumerat...
尝试这个: [(i, j) for i, j in enumerate(mylist)]
你需要将i,j放在一个元组中,让列表的理解工作。 或者,考虑到enumerate() 已经返回一个元组,您可以直接返回它,而无需首先解包: [pair for pair in enumerate(mylist)]
无论如何,返回的结果如预期的那样: > [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
Try this: [(i, j) for i, j in enume...
你应该evalPoly与evalPoly相同的参数调用evalPoly ,例如evalPoly([1,1,1,1],2) 当你调用evalPoly([3,1],2) ,它返回3 * 2 ^ 0 + 1 * 2 ^ 1,等于5。 You should call evalPoly with the same arguments as evalP, e.g. evalPoly([1,1,1,1],2) When you call evalPoly([3,1],2), it return 3*2^0 + ...
你可以使用下面的代码: import types
class A(object):
class B(object):
pass
class C(object):
pass
def enumerate_nested_classes(_class):
return [getattr(_class, n) for n in dir(_class) if not n.startswith('__')
and isins...
dict()构造函数接受一个iterable的key,value对序列。 您的代码段dict((1,2))无效,因为您传递的是元组(1,2) ,一个key,value对。 它迭代(1,2)并在期望序列的同时找到整数。 相反,你应该传递一个包含一对的元组: >>> dict(((1,2),))
{1: 2}
或者,例如,列表: >>> dict([[1,2]])
{1: 2}
enumerate()返回一个enumerate对象,它是元组对序列的迭代器: >>> enumerate(range(...
enumerate()不能倒数,只能倒数。 请改用itertools.count()对象 : from itertools import izip, count
for index, item in izip(count(len(data) - 1, -1), reversed(data)):
这会产生一个从长度(减1)开始的计数,然后沿着相反的顺序向下计数。 演示: >>> from itertools import izip, count
>>> data = ('spam', 'ham'...
使用dict.items()或dict.iteritems()方法遍历键和值; 后者允许您迭代而不构建键值对的中间列表: for date, data in a.iteritems():
print date,
for key, value in data.iteritems():
print '{}={}'.format(key, value),
直接在字典上循环可以为您提供密钥; 您仍然可以使用订阅来访问这些值: for date in a...
相当于my_dict.iteritems()的six.iteritems(my_dict)是six.iteritems(my_dict) ,所以: for count, (key, value) in enumerate(six.iteritems(my_dict), 1):
print key, value, count
The six.iteritems equivalent of my_dict.iteritems() is six.iteritems(my_dict), so: ...
使用row_number窗口函数: from pyspark.sql.functions import row_number
from pyspark.sql import Window
w = Window.partitionBy("some_column").orderBy("some_other_column")
df.withColumn("rn", row_number().over(w))
With row_number window function: from pyspark....
我觉得我在另一个答案上写了那个循环,但你想要做的正确答案是InvertedIndex[term].count(doc) 这将计算doc在列表中出现的次数。 I feel like I wrote that loop on another answer but the correct answer for what you want to do is InvertedIndex[term].count(doc) This will count the number of times that doc...
相关文章
Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重
...
python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速
...
Python的文件类型 Python有三种文件类型,分别是源代码文件、字节码文件和优化代码文件
源代
...
python的官网:http://www.python.org/ 有两个版本,就像struts1和st
...
好久没有写了,还不是近期刚过的期末考试和期中考试 最近因为一个微信公众平台大赛在学phthon 找了本
...
在python中, 去除了i > 0周围的括号,去除了每个语句句尾的分号,还去除了表示块的花括号。多出
...