我的代码有一些问题,这会很长,所以请告诉我。
我正在尝试创建一个“检查记录”函数,该函数接受 2D 列表 ( ID, account, operation, amount) 并返回列表中有效或无效项目的数量。这是基于设定的标准。每个标准也有自己的功能,即验证帐号是验证帐号列表的功能。
ID
account
operation
amount
然后检查记录函数将查看所有验证函数的结果,并从完整的 2D 列表中删除任何部分无效(返回 false)的项目。
完整列表如下所示(针对上下文):
[[‘SYD123’, ‘12823983’, ‘B’, ‘150.00’], [‘SYD127’, ‘12823983’, ‘D’, ‘20.00’], [‘BHS115’, ‘85849276’, ‘B’, ‘1000.85’], [‘BHS115’, ‘76530902’, ‘B’, ‘0.50’], [‘BMT251’, ‘49468141’, ‘W’, ‘500.00’], [‘DUB796’, ‘50175864’, ‘W’, ‘225.00’], [‘WGA584’, ‘34957765’, ‘D’, ‘2500.00’], [‘PMQ426’, ‘34957765’, ‘B’, ‘5700.00’]]
并像这样打印:
我遇到的问题是我总是得到4有效和0无效的返回。不管实际有多少。我相信这个问题是因为下面的变量只指向上面原始列表的一个元素。我需要它们成为具有相关变量的整个列吗?如果这是错误的,请指出我正确的方向。
4
0
我得到了什么:
Found (4) valid records. Found (0) invalid records.
它认为有效的项目(都应该有效):
[[‘SYD127’, ‘12823983’, ‘D’, ‘20.00’], [‘BHS115’, ‘76530902’, ‘B’, ‘0.50’], [‘DUB796’, ‘50175864’, ‘W’, ‘225.00’], [‘PMQ426’, ‘34957765’, ‘B’, ‘5700.00’]]
问题代码如下所示:
def check_records(log_records): #THESE VARIABLES!!!!! ID = log_records [0] account = log_records [1] operation = log_records [2] amount = log_records [3] #HERE!!!!!! overall_valid = False count = 0 #Use validate functions to get a valid or invalid record. validate_atm_id(ID) validate_account(account) validate_operation(operation) validate_amount(amount) if validate_atm_id == True and validate_account == True and validate_operation == True and validate_amount == True: overall_valid = True else: overall_valid = False for elem in log_records: if overall_valid == False: count + 1 log_records.remove(elem) total_records = len(log_records) invalid_records = count valid_records = total_records - invalid_records print(f"Found ({valid_records}) valid records.") print(f"Found ({invalid_records}) invalid records.") print(" ")
验证功能之一(用于额外的上下文):
def validate_account(account): valid = False #Account number must be 8 digits long. if len(account) != 8: valid = False #Account number must only contain numbers. elif not account.isdigit(): valid = False #If the account number meets all requirments then it is valid. else: valid = True if valid == True: return True elif valid == False: return False
我已经为此工作了一段时间,这是我需要为这段代码弄清楚的最后一件事(它比这部分要大得多,而且还可以做其他事情)。我对此还是很陌生,所以如果有什么太令人困惑,我很抱歉。任何帮助都会非常棒。
!!!!!更新!!!!!!我的代码现在可以正确计算列表中有效和无效项目的数量。但是,无论我做什么,它都拒绝将 4 或多或少视为有效。即使我强制我的函数返回一个错误值,这应该使列表中的所有项目都无效。我的代码现在是:
for elem in log_records: if (validate_atm_id(ID) == True and validate_account(account) == True and validate_operation(operation) == True and validate_amount(amount) == True): overall_valid = True for elem in log_records: if not (validate_atm_id(ID) == True and validate_account(account) == True and validate_operation(operation) == True and validate_amount(amount) == True): overall_valid = False if overall_valid == False: count += 1 log_records.remove(elem) #The number of valid and invalid records is calculated and printed to the screen. valid_records = len(log_records) invalid_records = count print(f"Found ({valid_records}) valid records.") print(f"Found ({invalid_records}) invalid records.") print(" ")
更改此部分是唯一对有效或无效类有任何影响的事情,这使我认为问题必须存在于某个地方。即使弄乱我用来验证的函数也不会改变结果。它甚至似乎都没有使用它们,否则无论如何它们都会返回 true。
您可以使用列表推导创建类似列的结构:
ids = [i[0] for i in log_records] # assumming log_records is your full list accounts = [i[1] for i in log_records] operations = [i[2] for i in log_records] amounts = [i[3] for i in log_records]
虽然我不建议这样做,因为您要检查的是单个记录,并且如果您创建类似列的变量,您将不得不处理大量索引并且有点痛苦。
这也是不必要的长:
if validate_atm_id == True and validate_account == True and validate_operation == True and validate_amount == True: overall_valid = True
我将其重写为:
overall_valid = True if not any((validate_atm_id, validate_account, validate_operation, validate_amount )): overall_valid = False
这看起来像一个错字:
for elem in log_records: if overall_valid == False: count + 1 # this line shouldn't work and should be "count += 1" log_records.remove(elem)