我有以下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
你忘記了'small'的文件擴展名。 – TerryA 2013-04-07 09:00:46
line_one和line_two的值是什麼? – alecxe 2013-04-07 09:01:29
@Haidro我使用Ubuntu,因此它是我的文件的名稱。 – 2013-04-07 09:06:02