Python循环追加替换过去的变量

时间:2017-04-27 12:52:01

标签: python

我有代码:

fileAspect = []

id_filename = -1
for filename in glob.glob(os.path.join(path, '*.dat')):
    id_filename += 1
    f = open(filename, 'r')
    content = f.read()
    soup = BeautifulSoup(content, 'html.parser')
    all_paragraph = (soup.find_all('content'))
    result_polarity = []

    for all_sentences in all_paragraph:
        sentences = nlp.parse(all_sentences.get_text())
        for sentence in sentences['sentences']:
            for words in sentence['dependencies']:
                if(words[1]<>"ROOT"):
                    result_proccess = proccess1(myOntology, words)                  if(result_proccess):
                        result_polarity.append(words)
                        ini+=1
                    else:
                        out+=1
    ont = ''
    ont = myOntology

    for idx_compare1 in range(len(ont)):
        for idy in range(len(result_polarity)):
            for idw in range(len(result_polarity[idy])):
                if(result_polarity[idy][1]<>"ROOT" and idw<>0):
                    try:
                        closest = model.similarity(ont[idx_compare1][0], result_polarity[idy][idw])
                        if closest >= 0.1 :
                            valpolar = valuePol(result_polarity[idy][idw])
                            if(valpolar==1):
                                ont[idx_compare1][2]+=1
                            elif(valpolar==2):
                                ont[idx_compare1][3]+=1
                            tp += 1
                            break
                    except Exception as inst:
                        fn += 1
                        break

    print "^^^^^^^^^^^^^"
    print id_filename
    print ont
    fileAspect.append(ont)

print 'overall'
#print fileAspect
for aspek in fileAspect:
    print aspek
    print "---"

为什么结果会替换以前的数据

我希望样本

a = []

a.append('q') 
a.append('w') 
a.append('e') 

print a

结果是: [ 'Q', 'W', 'E']

但是我以前的代码得到了: [ 'E', 'E', 'E']

所以问题是,当我追加变量数组“fileAspect”时,它不仅附加了,而且还替换了以前的代码。我使用简单的代码,它很好,但是当它出现在我的代码中时,我非常困惑..超过1小时我尝试但是...

谢谢,任何帮助我都非常赞成..

0 个答案:

没有答案