Python TypeError:“ str”和“ float”的实例之间不支持“ <”

时间:2018-08-30 13:07:04

标签: python python-3.x

我使用此代码将数据插入数据库。

但是当代码在for cmp_noun, value in data_collection_member.most_common():行运行时

错误消息是TypeError: '<' not supported between instances of 'str' and 'float'

data_collection_member中的数据如下:

{
'A I':[12, 122.55709829131442, '名詞-一般',''],
'G o o g l e':[1, 2.719033941502371, '名詞-一般', ''],
'I C':[1, 2.719033941502371, '名詞-一般', '-0.6656']
} 

我的代码:

def insert_report_keyword_property(server, user, password, database, employee_list, report_year, report_week):
    if employee_list:
        try:
            conn = pymssql.connect(server, user, password, database)
            cur = conn.cursor()
            for employee in employee_list:
                employee_report = read_report_from_database(server, user, password, database, report_year,report_week,employee)
                if employee_report:
                    frequency_member = cmp_noun_dict(employee_report)
                    LR_member = score_lr(frequency_member, ignore_words=IGNORE_WORDS, lr_mode=1, average_rate=1)
                    term_imp_member = term_importance(frequency_member, LR_member)
                    data_collection_member = collections.Counter(term_imp_member)
                    key_words_list_memeber = []
                    for cmp_noun, value in data_collection_member.most_common():
                        para_keyword = "'" + modify_agglutinative_lang(cmp_noun) + "'"
                        para_importance_degree = value[1]
                        para_keyword_frequency = value[0]
                        para_free1 = "'" + value[2] + "'"
                        sql = ' insert into report_keyword_property (report_year, report_week, employee_code, keyword, importance_degree, keyword_frequency, free1 ) ' \
                              ' values(%s, %s, %s, %s, %s, %s, %s ) ' \
                              % (report_year, report_week, employee,para_keyword, para_importance_degree, para_keyword_frequency, para_free1)
                        cur.execute(sql)
                        conn.commit()
        except pymssql.Error as ex:
            logger.error("dbException:" + str(ex))
            raise ex
        except Exception as ex:
            logger.error("Exception:" + str(ex))
            conn.rollback()
            raise ex
        finally:
            conn.close()

如何修复我的代码?

当我使用它进行测试时,没有错误,为什么?

import collections
a={'A I':[12, 122.55709829131442, '名詞-一般',''],
'G o o g l e':[1, 2.719033941502371, '名詞-一般', ''],
'I C':[1, 2.719033941502371, '名詞-一般', '-0.6656']
}

b = collections.Counter(a)
for key, value in b.most_common():
   print(key,value)

2 个答案:

答案 0 :(得分:0)

如果您在演示中将键TypeError: '<' not supported between instances of 'str' and 'float'下列表中的最后一项从'I C'更改为'-0.6656',则能够重现您的-0.6656错误,因此您可能在问题中输入了错字,因为您可能不打算将该数字键入为字符串。

如果您打算将最后一项与数字进行比较,则应确保term_importance()(其中term_imp_member获得其值)将最后一项输出为数字或{{1} },以便它们具有可比性。

答案 1 :(得分:0)

我这样修改了我的代码,然后没有错误。 谢谢大家!

for cmp_noun in data_collection_member:
    para_keyword = "'" + modify_agglutinative_lang(cmp_noun) + "'"
    para_importance_degree = data_collection_member[cmp_noun][1]
    para_keyword_frequency = data_collection_member[cmp_noun][0]
    para_pn = data_collection_member[cmp_noun][3]
    para_free1 = "'" + data_collection_member[cmp_noun][2] + "'"