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
Since you traverse
datas
after runningrange()
, and sincerange()
is an iterative function, when running thefor
loop ofdatas
, the value ofi
is always 60303 , so the condition ofif 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 thisHere you can directly determine whether
temp_dict[word_index]
exists as 0. If it is not defined, it should beNone
, so this block is changed to