简体   繁体   English

python 列表在迭代 14 时超出范围?

[英]python list is out of range at iteration 14?

i have a list of indexes that point a term in database我有一个索引列表,指向数据库中的一个术语

Here is my code:这是我的代码:

for doc in TSS:
    tokens = word_tokenize(doc[1])
    tokens = [token.lower() for token in tokens if len(token) > 2]
    tokens = [token for token in tokens if token not in words]
    tokens = [stemmer.stem(token) for token in tokens]
    final_tokens.extend(tokens)
    sql = """SELECT id_term FROM tss_terms WHERE tss_terms.term IN %s"""
    pram = [final_tokens]
    cursor.execute(sql, pram)
    ids = list(cursor.fetchall())
    count = 0
    data[doc[1]] = {'tf': {}, 'ntf': {}}
    for token in final_tokens:
        data[doc[1]]['tf'][token] = freq(token,final_tokens)
        data[doc[1]]['ntf'][token] = tf(token,final_tokens)
        query = """INSERT INTO tss_term_freq(id_term,id_doc,tf,normalized_tf)VALUES(%s,%s,%s,%s)"""
        param = [ids[count],doc[0],data[doc[1]]['tf'][token],data[doc[1]]['ntf'][token]]
        #cursor.execute(query, param)
        count = count + 1
    del final_tokens[:]
    print count

the ids list always get error : list index out of range at the 14th iteration. ids列表总是error : list index out of range在第 14 次迭代时error : list index out of range

the values of TSS[13] are like this: TSS[13] 的值是这样的:

(5927L, 'PERHITUNGAN POHON KELAPA SAWIT PADA CITRA FOTO UDARA\r\nYANG BERBASIS BENTUK MAHKOTA POHON')

then the values of TSS[14] are like this:那么 TSS[14] 的值是这样的:

(6698L, 'SINKRONISASI WAKTU METER LISTRIK BERDASARKAN GLOBAL POSITIONING SYSTEM (GPS) PADA SUPERVISORY CONTROL AND DATA ACQUISITION (SCADA) BERBASIS WEB')

TSS contains document ids and titles of thesis in my university TSS 包含我所在大学的文档 ID 和论文标题

can anyone show my mistake and fix it?任何人都可以显示我的错误并修复它吗?

Please help :(请帮忙 :(

The way the list "id" is declared is wrong.声明列表“id”的方式是错误的。 I didnt get what you are doing there.我不明白你在那里做什么。 But if want to store all the values returned by fetchall() function then use for loop for doing it.但是如果想要存储 fetchall() 函数返回的所有值,那么使用 for 循环来完成它。 Declare an empty list id.声明一个空列表 ID。 id = [ ] Use a for loop for taking all the values. id = [ ] 使用 for 循环获取所有值。 And to save the value in list use id.append(value)并将值保存在列表中使用 id.append(value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM