0

I have code :

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 "---"

Why the result is replace previous data

sample i hope :

a = []

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

print a

the result is : ['q','w','e']

but with my previous code i got : ['e','e','e']

so the problem is, when i append in variable array "fileAspect", it's didn't only append but also replace the previous code. i use simple code and it's fine, but when it's come in my code, i very confused.. more than 1 hour i try but..

Thanks, any help i'am very apreciated..

Budi Mulyo
  • 384
  • 5
  • 22
  • Try to add `.copy()` after your variable in the append – Nuageux Apr 27 '17 at 12:55
  • the first result : fileAspect.append(ont).copy()) ^ SyntaxError: invalid syntax. The second result : fileAspect.append(ont).copy() AttributeError: 'NoneType' object has no attribute 'copy' – Budi Mulyo Apr 27 '17 at 13:00
  • I should have written it entirely: `fileAspect.append(ont.copy())` – Nuageux Apr 27 '17 at 13:03
  • need import copy;? still error : fileAspect.append(ont.copy()) AttributeError: 'list' object has no attribute 'copy' – Budi Mulyo Apr 27 '17 at 13:06
  • It seems that your `ont = None` as some point. And `None`can't be copy. – Nuageux Apr 27 '17 at 13:15
  • can u explain more,, i don't understand.. :( – Budi Mulyo Apr 27 '17 at 13:21
  • `.copy()` can't deal with variable equals to `None`. Somewhere in your for, your variable `ont` seems to took this value. A solution to avoid that could be to use : `import copy` `fileAspect.append(copy.deepcopy(ont))` I'm not entirely sure this will solve your problem, but as i can't test on your code... – Nuageux Apr 27 '17 at 13:33
  • You might also have a look [here](http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list), it is a similar problem. – Nuageux Apr 27 '17 at 13:35

0 Answers0