如果搜索多个单词,则创建一个集合是有意义的:print(set(brown_sents).intersection(zip(repeat(most_ambiguous_word),
word_class_dict[most_ambiguous_word])))
输出{('word2', 'wordclass2'), ('word2', 'wordclass3')}
要理解它的作用,请将脚本保存到一个文件中,例如search-word.py,然后运行:$ python -i search-word.py
它显示Python提示符:>>>
您可以尝试单个表达式来查看它们的作用,例如:>>> zip(repeat('a'), [1,2,3])
[('a', 1), ('a', 2), ('a', 3)]
>>> set('abcaadeff')
set(['a', 'c', 'b', 'e', 'd', 'f'])
>>> set('abcaadeff').intersection('abc')
set(['a', 'c', 'b'])
查看帮助:>>> help(zip)
Help on built-in function zip in module __builtin__:
zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence.
按q退出。如果个别帮助信息不清晰:>>> help(repeat)
Help on class repeat in module itertools:
class repeat(__builtin__.object)
| repeat(element [,times]) -> create an iterator which returns the element
| for the specified number of times. If not specified, returns the element
| endlessly.
...[snip]...
请尝试查看模块的联机帮助:>>> module = 'itertools'
>>> import webbrowser
>>> webbrowser.open('http://docs.python.org/library/' + module)
找到^{}函数。在
简而言之:阅读文档,在提示下尝试一些代码,重复。如果你卡住了,ask question。在