0
我需要帮助减少下面的代码的圈复杂度:的Python:降低圈复杂度
def avg_title_vec(record, lookup):
avg_vec = []
word_vectors = []
for tag in record['all_titles']:
titles = clean_token(tag).split()
for word in titles:
if word in lookup.value:
word_vectors.append(lookup.value[word])
if len(word_vectors):
avg_vec = [
float(val) for val in numpy.mean(
numpy.array(word_vectors),
axis=0)]
output = (record['id'],
','.join([str(a) for a in avg_vec]))
return output
例输入:
record ={'all_titles': ['hello world', 'hi world', 'bye world']}
lookup.value = {'hello': [0.1, 0.2], 'world': [0.2, 0.3], 'bye': [0.9, -0.1]}
def clean_token(input_string):
return input_string.replace("-", " ").replace("/", " ").replace(
":", " ").replace(",", " ").replace(";", " ").replace(
".", " ").replace("(", " ").replace(")", " ").lower()
所以一切都存在于lookup.value的话,我正在考虑它们的矢量形式的平均值。
你介意解释代码试图在第一个地方做什么? –
增加了一些更多的细节 – futurenext110
我试着从一开始就编码这个自己,我结束了相同的代码:) –