我有以下 python 脚本,我想交替地从两个迭代器中获取值。
filename = "small"
with open(filename,'r') as plot_data:
main_dict = dict()
line_one = itertools.islice(plot_data, 0, None, 4)
line_two = itertools.islice(plot_data, 2, None, 4)
dictionary = defaultdict(list)
#take values from iterators alternatively.
for movie_name, movie_plot in itertools.izip(line_one, line_two):
movie_plot = movie_plot.lower()
words = re.findall(r'\w+', movie_plot, flags = re.UNICODE | re.LOCALE)
elemStopW = filter(lambda x: x not in stopwords.words('english'), words)
#list of words.
print elemStopW
for word in elemStopW:
word = PorterStemmer().stem_word(word)
dictionary[movie_name].append(word)
main_dict[word] = len(main_dict)
print main_dict
该脚本不打印任何内容。我不明白为什么。我不想合并迭代器,因为我想在同一个循环中使用这两个值。
任何帮助表示赞赏。
编辑:为了避免一些许可(如评论)。以下脚本工作正常
filename = "small"
with open(filename,'r') as plot_data:
main_dict = dict()
line_one = itertools.islice(plot_data, 0, None, 4)
dictionary = defaultdict(list)
for movie_name in line_one:
print movie_name