我的函数获取表单字典的表,并在应用该人询问的查询后返回表。我还有其他几种方法,但我遇到了等条件方法的问题。这就是我尝试过的。
class Table():
def where_con(self,table,conditions):
for condition in conditions:
if ('='in condition):
print(table.get_dict())
print(condition)
self = table.equal_condition(table,condition)
return(self)
elif('>'in condition):
new_table = table.greater_condition(table, condition)
return(self)
def equal_condition(self,table,condition):
'''(Table, string) -> Table
This Function takes in a table and a condition and applies the
condition and returns a new table with the condition applied
REQ: Table must have the contents of the condition
REQ: The condition must have proper syntax
REQ: The condition must contain the equal sign in string form
'''
number_rows = table.num_rows()
print(number_rows)
dictionary = table.get_dict()
print(dictionary)
condition = condition.split('=')
print(condition)
#new_table = Table()
# Adding Coloums Name in Self
for col in dictionary:
self.add_column({col: []})
# If the Second part is a string
if ("'" in condition[1]):
condition[1] = condition[1].strip("'")
i=0
while(i<number_rows):
print(i)
i=i+1
if (dictionary[condition[0]][i] == condition[1]):
for key in self.get_dict():
self = self.update_column(key,dictionary[key][i])
#i=i+1
else:
i=0
while(i<number_rows):
print(i)
if (dictionary[condition[0]][i] == dictionary[condition[1]][i]):
for key in self.get_dict():
self.update_column(key,dictionary[key][i])
i=i+1
return self
所以当我输入
时>>>a = Table()
>>>a.set_dict({'w.critic_rating': ['5', '5', '5', '5'], 'o.for': ['Directing', 'Acting', 'Directing', 'Acting'], 'w.title': ['Titanic', 'Titanic', 'Avatar', 'Avatar'], 'o.title': ['Avatar', 'Titanic', 'Avatar', 'Titanic'], 'w w.your_rating': ['4.5', '4.5', '5', '5'], 'w.average_rating': ['4.75', '4.75', '5', '5']})
>>>d = Table()
>>>f = where_con(a,"w.title=o.title")
4
{'o.for': ['Directing', 'Acting', 'Directing', 'Acting'], 'o.title': ['Avatar', 'Titanic', 'Avatar', 'Titanic'], 'w.critic_rating': ['5', '5', '5', '5'], 'w.your_rating': ['4.5', '4.5', '5', '5'], 'w.average_rating': ['4.75', '4.75', '5', '5'], 'w.title': ['Titanic', 'Titanic', 'Avatar', 'Avatar']}
['w.title', 'o.title']
0
Traceback (most recent call last):
Python Shell, prompt 2, line 1
File "C:\Users\Abhinav\Desktop\MAde_answer\database.py", line 205, in <module>
if (dictionary[condition[0]][i] == dictionary[condition[1]][i]):
builtins.IndexError: list index out of range
为什么会发生这种情况,我该如何解决?任何帮助表示赞赏。