提出问题有助于澄清问题;这是一个功能,可以更多地了解我正在尝试做的事情。有没有办法表示上面¶2中的一些东西,或者在SQL或python中做我想要完成的更有效的方法show_distinct
?
#!/usr/bin/env python
import sqlite3
conn = sqlite3.connect('so_question.sqlite')
cur = conn.cursor()
cur.execute('create table soquestion (word, big, expensive, smart, fast)')
cur.execute("insert into soquestion values ('dog', 9, -10, -20, 4)")
cur.execute("insert into soquestion values ('professor', 2, 4, 40, -7)")
cur.execute("insert into soquestion values ('ferrari', 7, 50, 0, 48)")
cur.execute("insert into soquestion values ('alaska', 10, 0, 1, 0)")
cur.execute("insert into soquestion values ('gnat', -3, 0, 0, 0)")
cur.execute("select * from soquestion")
all = cur.fetchall()
definition_list = ['word', 'big', 'expensive', 'smart', 'fast']
def show_distinct(db_tuple, def_list=definition_list):
minimum = min(db_tuple[1:])
maximum = max(db_tuple[1:])
if abs(minimum) > maximum:
print db_tuple[0], 'is not', def_list[list(db_tuple).index(minimum)]
elif maximum > abs(minimum):
print db_tuple[0], 'is', def_list[list(db_tuple).index(maximum)]
else:
print 'no distinct value'
for item in all:
show_distinct(item)
运行这个给出:
狗不聪明
教授很聪明
法拉利很贵
阿拉斯加很大
蚊子不大
>>>