I want to set some data in the file to the original value, and set the missing value to 0, but the final result is all 0, what is wrong with the code?
Original data picture description
Processing results image description
#!/usr/bin/python
#coding:utf-8
train_data = {}
input_data = open("train_tfidf.txt", "r").readlines()
output_data = open("single_tfidf.txt", "w")
for line in input_data:
temp_dict = {}
for i in range(60304):
temp_dict[i] = 0
datas = line.split()
for ele in datas:
try:
word_index = ele.split(":")[0]
tfidf = ele.split(":")[1]
if word_index == i:
temp_dict[i] = tfidf
except:
continue
# print temp_dict
# print word_index, tfidf
output_data.write(str(temp_dict))
output_data.write('\n')
[1]: /img/bVPJMi
[2]: /img/bVPJMV
PHP中文网2017-06-28 09:27:55
Since you traverse datas
after running range()
, and since range()
is an iterative function, when running the for
loop of datas
, the value of i
is always 60303 , so the condition of if word_index == i
is not satisfied, so except for the 60303 item, the others are still initial values. In fact, it is recommended to change it like this
if word_index == i:
temp_dict[i] = tfidf
Here you can directly determine whether temp_dict[word_index]
exists as 0. If it is not defined, it should be None
, so this block is changed to
if temp_dict[word_index] == 0:
temp_dict[word_index] = tfidf