【发布时间】:2015-03-16 22:55:13
【问题描述】:
我一直在寻找其他问题,但没有找到任何帮助。 我得到了这个代码:
def pairs(self, listOrString):
if listOrString:
return filter(re.compile(self.pairwise(self.text)).match, frozenset(self.text))
else:
return ' '.join(filter(re.compile(self.pairwise(self.text)).match, frozenset(self.text)))
def pairs_freqency(self):
return Counter(self.pairs(True))
def sum_pairs(self):
return len(self.ngrams(self.letters(list),2))
def pair_probability(self):
{pair : freqency / self.sum_pairs() for (pair, freqency) in self.pairs_freqency().iteritems()}
def pairwise(self, sequence):
x,y = tee(sequence)
next(y)
return zip(x,y)
但是当我尝试打印时:
print pairs_freqency()
我收到此错误:
**更新
Traceback (most recent call last):
File "...", line 281, in <module>
print pairs(string, text)
File "...", line 46, in get_pairs
return filter(re.compile(self.pairwise(self.text)).match, frozenset(self.text))
File "...", line 190, in compile
return _compile(pattern, flags)
File "...", line 232, in _compile
p = _cache.get(cachekey)
TypeError: unhashable type: 'list'
谁能尽快帮助我。
谢谢。
【问题讨论】:
-
你肯定会让任何人尽可能难以真正帮助你(更不用说“尽可能快”),通过点点滴滴地揭示你的代码,而且总是部分地。现在我们知道
pairwise返回一个列表(这就是zip在 Python 2 中返回的内容)并且您正在尝试re.compile一个列表,这是不可能的,尽管错误 should 是TypeError: first argument must be string or compiled pattern。如果可能的话,你想通过“重新编译一个列表”来完成什么——你对这样的编译有什么期望(错误地)?! -
成功了! :-) 非常感谢。
标签: python list python-2.7 error-handling typeerror